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!
Printable View
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?
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.
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.Quote:
Originally Posted by pheres
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.Quote:
Originally Posted by pheres
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.
B is no member of A. B is an inner class to A.