Answer the question
In order to leave comments, you need to log in
Why can't you declare stack objects of outer classes for inner classes?
For certain code, there is a need to use the objects of the outer class for the inner class.
Here is what is going to be:
class Outer {
public:
class Inner {
Outer *_outer;
public:
Inner() : _outer(new Outer()) { }
};
Outer() { }
};
class Outer {
public:
class Inner {
Outer _outer;
public:
Inner() : _outer() { }
};
Outer() : _inner() { }
};
Answer the question
In order to leave comments, you need to log in
A class is considered complete when the closing parenthesis is reached when it is parsed. When declaring a nested class inside an outer class, it is not yet complete and you cannot declare a member of its type. You can create a pointer to an incomplete type, so the first option compiles.
You can fix it like this:
struct Outer
{
struct Inner;
};
struct Outer::Inner
{
Outer _outer;
Inner() : _outer() { }
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question