I have
class outer: public base
{
public:
class inner
{
foo();
}
}
- How do i initialize these nested class?
- Can I call foo() from the outer class?
- Can you please tell me what is the thumb rule I should know while nesting class and accessing the members?
Thank you
I guess you’re coming from java.
A c++ nested struct/class is like a java
staticnested type. It has no instance-relation with the containing type.In fact you should be able to move the inner class to the containing namespace with no other changes than the potential need to mark the former inner class as a friend of the outer class.
See for demonstration http://ideone.com/ddjGX
You don’t. You can only initialize members (class instances)
Not unless you are a friend or the method (foo()) is public
I’d choose a nested type only if
I illustrated these ‘rules of thumb’ below. Note how the nested type has transparent access to private members of the outer class.