--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
No. Of course not. You're saying an A contains an A contains an A
contains an A ... --> endless recursion). If you remove members a and
b or turn them into pointers, it'll be fine. The member p is not an
issue here.
Cheers,
SG
Of course you can't. Just stop and think. Each of a and b in the above
would have to have their own members var, a, b and p. IOWs a must have
its own a which must have its own a which must have its own a ...
You can have a pointer or a reference to the class because neither of
those needs their own instance variables.
You can make a pointer to an instance of the class you are defining, but
you can't make an instance of the class within the class. The compiler
would have no way of figuring out what size to make that object, or it
would be of infinite size.
What are you trying to do?
LR
Simple. How big is an A? Well, let's see. It's the sizeof int +
sizeof A* + 2 * sizeof A.
Well, now we take that sizeof A and work it again.
So it's sizeof int + sizeof A* + 2 * (sizeof int + sizeof A* + 2 *
sizeof A). Which recurses infinitely.
Note that in your code, the declaration of A::p is OK, because only a
*pointer* is being allocated.
No.
Apart from anything else - What is sizeof(A)
As you can see pointers and references are OK.
What would the size of an instance of this class be?
James
If you think about what you are trying to do, it might not be that
surprising. You are creating an A, which has an A as an internal part
of it. That internal A, by definition, has an A internal to it. That
A has yet another A... to infinity. If A declared without an A inside
it took at least one bit to represent, then A with an A inside it
would take infinite gigabytes to represent.
Note that you CAN have an A that has a pointer (or reference) to an A
in it:
class A
{
A * another_a;
How is that supposed to work? C++'s variables are embedded, not
references. A would have infinite size, as every A would contain yet
another A.
> can't i declare class type variable inside its declaration for
Yes - you can't.
> example:
> class A{
> int var;
> A a;
> A b;
> A *p;
> }
What do you think sizeof(A) should evaluate to?
--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---
> can't i declare class type variable inside its declaration for
> example:
> class A{
> int var;
> A a;
> A b;
> A *p;
> }
>
You only can if it is static:
class A {
static A a; // works
};
But if it's a non-static member, it is a definition (not merely a
declaration), and type "A" is still incomplete at that point, and thus it
errors out. From a pratical point, it cannot work either, because the object
would have an inifitely large size (including itself all again).
You can have it be a pointer or reference to A, though.
Suppose you could. Then when you instantiate an instance of A,
it contains an A, which contains an A, which contains an A...
when does it end? (Or would this object be infinitely big?)
In reality, while in the body of A's class definition, A is
considered an incomplete type, so you can't store objects of
a type in itself.
You can, however, store POINTERS to A.
Chris