cascaded struct members.

S

sunny

Hi All

why does forward declaration does not work in following code.

struct A;
struct B
{
struct A a;

// struct A *a // WORKS SINCE ITS POINTER
};

struct A
{
struct B b;
};

gives compiler error for incomplete type A. why does forward
declaration of Struct A solve this problem.
 
R

red floyd

sunny said:
Hi All

why does forward declaration does not work in following code.

struct A;
struct B
{
struct A a;

// struct A *a // WORKS SINCE ITS POINTER
};

struct A
{
struct B b;
};

gives compiler error for incomplete type A. why does forward
declaration of Struct A solve this problem.

1. How big is A? The compiler doesn't know at the point you declare A a
in B.

2. How big is an A at the end of your code? If you were allowed to
have an A contain a full B, and vice versa, then it's infinitely sized:

A contains a B which contains an A which contains a B which ....

3. Works when a is an A* because the size of a pointer is well-defined.
 
V

Victor Bazarov

sunny said:
why does forward declaration does not work in following code.

struct A;
struct B
{
struct A a;

Because the compiler NEEDS TO KNOW the internals of 'A' here.
// struct A *a // WORKS SINCE ITS POINTER

All pointers to objects are of the same size. The compiler does
NOT need to know the internals of 'A' here.
};

struct A
{
struct B b;
};

gives compiler error for incomplete type A. why does forward
declaration of Struct A solve this problem.

The last statement is false. A forward declaration of 'A' does
NOT solve the problem of mutual containment. Nothing can. If
each object 'B' contains a member of type 'A', and, in turn, each
object 'A' contains a member of type 'B', what's the size of the
objects?

V
 
R

Ron Natalie

sunny said:
Hi All

why does forward declaration does not work in following code.

struct A;
struct B
{
struct A a;

You must have a complete definition here since you actually
allocate one.
// struct A *a // WORKS SINCE ITS POINTER
};

struct A
{
struct B b;
};

gives compiler error for incomplete type A. why does forward
declaration of Struct A solve this problem.

You have A containing a B which contains an A which contains a B ...

How big is this object?
 
B

BobR

Ron Natalie wrote in message
You have A containing a B which contains an A which contains a B ...

How big is this object?

Same size as the universe + 1.

I'm running the calculation on my computer now...... uhh....might be a while.
I'll get back to you on this when it's done...
<millions of years pass>
Ahhh, 1% done.... won't be long now...
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

BobR said:
Same size as the universe + 1.

I'm running the calculation on my computer now...... uhh....might be a
while. I'll get back to you on this when it's done...
<millions of years pass>
Ahhh, 1% done.... won't be long now...

Your computer is broken. Any decent computer after that long calculation
must tell that the answer is 42.
 
B

BobR

Your computer is broken. Any decent computer after that long calculation
must tell that the answer is 42.

I just checked with Duck Dogers (remember Daffy Duck?) in the year 2525, and
he said the output was:
42e+std::numeric_limits<long long>::max()
....as the calculation passed through his time period. So, obviously YOU
misplaced a decimal point!!
[ gads, you'd think with a 1024bit 7.2TeraHz CPU (low end machine) the 'long
long' would be deprecated by then!! ]

Yup, that duck really quacks me up!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top