Hi,
for example
Is there any way around?Code:class A { class B { A mA; // VS complains that A is undefined } };
Thank you in advance!
Hi,
for example
Is there any way around?Code:class A { class B { A mA; // VS complains that A is undefined } };
Thank you in advance!
Why does B need an A object as a member?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
A defines a structure and B defines some presentation, read- and write functions to it. Therefore B uses some state (member) variables which shall not be member of A, so it needs to be an own class.
I could of course just do
...which would rather be the same, at least for my. Why not for the compiler?Code:class A { }; class B { A mA; // VS complains that A is undefined }
I would like to use the inner class because this way it's in the namespace defined by the outer class.
This still doesn't explain why B needs an instance of A, rather than storing (or being passed) a reference/pointer to an A.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
The data is stored on a server, B need to receive it. You can't hold remote data per reference.
It sounds as if B is redundant and you should move the member functions of B to A.Originally Posted by pheres
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
That means B's members functions depend on each other, so B needs it's own member variables. But the can't be part of A because A is needed by legacy code as well and this code relies on the memory layout of A.
I guess there is no solution to the original problem and I've to split A and B.
Yeah, perhaps as a workaround you can put them both in the same namespace.Originally Posted by pheres
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Short answer: No. If A has B as a member, B cannot have A as a member. Why? It would be infinite recursion. B can have a pointer to type A, but it cannot have an instance of A.
Quzah.
Hope is the first step on the road to disappointment.
B is no member of A. B is an inner class to A.