Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

can't i declare class type variable inside its declaration

0 views
Skip to first unread message

vicky

unread,
Dec 23, 2009, 11:55:55 AM12/23/09
to
can't i declare class type variable inside its declaration for
example:
class A{
int var;
A a;
A b;
A *p;
}

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

SG

unread,
Dec 24, 2009, 3:58:22 AM12/24/09
to
On 23 Dez., 17:55, vicky <mehta...@gmail.com> wrote:
> can't i declare class type variable inside its declaration for
> example:
>
> class A {
> int var;
> A a;
> A b;
> A *p;
> };

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

Francis Glassborow

unread,
Dec 24, 2009, 3:59:19 AM12/24/09
to
vicky wrote:
> can't i declare class type variable inside its declaration for
> example:
> class A{
> int var;
> A a;
> A b;
> A *p;
> }
>

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.

LR

unread,
Dec 24, 2009, 4:01:20 AM12/24/09
to
vicky wrote:
> can't i declare class type variable inside its declaration for
> example:
> class A{
> int var;
> A a;
> A b;
> A *p;
> }
>


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

red floyd

unread,
Dec 24, 2009, 4:02:41 AM12/24/09
to
On Dec 23, 8:55 am, vicky <mehta...@gmail.com> wrote:
> can't i declare class type variable inside its declaration for
> example:
> class A{
> int var;
> A a;
> A b;
> A *p;
>
> }
>

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.

Nick Hounsome

unread,
Dec 24, 2009, 4:03:16 AM12/24/09
to
On 23 Dec, 16:55, vicky <mehta...@gmail.com> wrote:
> can't i declare class type variable inside its declaration for
> example:
> class A{
> int var;
> A a;
> A b;
> A *p;
>
> }

No.

Apart from anything else - What is sizeof(A)

As you can see pointers and references are OK.

analizer

unread,
Dec 24, 2009, 4:03:00 AM12/24/09
to
On Dec 23, 7:55 pm, vicky <mehta...@gmail.com> wrote:
> can't i declare class type variable inside its declaration for
> example:
> class A{
> int var;
> A a;
> A b;
> A *p;
>
> }
>
can't.
Class members is being created by object c'tor, so in your example
you'll have:
C'tor creates member "a", i.e. calls "a" c'tor. "a" c'tor creates
"a.a" member, i.e. calls "a.a" c'tor and on and on and on...
As the result you'll have structure of infinite size which c'tor falls
into infinite recursion.

James Lothian

unread,
Dec 24, 2009, 4:00:28 AM12/24/09
to
vicky wrote:
> can't i declare class type variable inside its declaration for
> example:
> class A{
> int var;
> A a;
> A b;
> A *p;
> }
>

What would the size of an instance of this class be?

James

John H.

unread,
Dec 24, 2009, 4:02:01 AM12/24/09
to
On Dec 23, 10:55 am, vicky <mehta...@gmail.com> wrote:
> can't i declare class type variable inside its declaration

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;

CornedBee

unread,
Dec 24, 2009, 4:01:29 AM12/24/09
to
On Dec 23, 5:55 pm, vicky <mehta...@gmail.com> wrote:
> can't i declare class type variable inside its declaration for
> example:
> class A{
> int var;
> A 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.

Thomas Maeder

unread,
Dec 24, 2009, 4:02:52 AM12/24/09
to
vicky <meht...@gmail.com> writes:

> 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 ---

Johannes Schaub (litb)

unread,
Dec 24, 2009, 4:00:18 AM12/24/09
to
vicky wrote:

> 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.

Chris Uzdavinis

unread,
Dec 24, 2009, 4:01:03 AM12/24/09
to
On Dec 23, 10:55 am, vicky <mehta...@gmail.com> wrote:
> can't i declare class type variable inside its declaration for
> example:
> class A{
> int var;
> A a;
> A b;
> A *p;
> }

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

0 new messages