Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   cascaded struct members. (http://www.velocityreviews.com/forums/t457655-cascaded-struct-members.html)

sunny 10-16-2006 03:56 PM

cascaded struct members.
 
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.


red floyd 10-16-2006 04:00 PM

Re: cascaded struct members.
 
sunny wrote:
> 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.


Victor Bazarov 10-16-2006 04:01 PM

Re: cascaded struct members.
 
sunny wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Ron Natalie 10-16-2006 05:35 PM

Re: cascaded struct members.
 
sunny wrote:
> 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?

BobR 10-16-2006 09:55 PM

Re: cascaded struct members.
 

Ron Natalie wrote in message
<4533c2bf$0$5922$9a6e19ea@news.newshosting.com>. ..
>sunny wrote:
>> Hi All
>>
>> why does forward declaration does not work in following code.
>>
>> struct A;
>> struct B{
>> struct A a;
>> };
>>
>> 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?


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

--
Bob <G> R
POVrookie



=?ISO-8859-15?Q?Juli=E1n?= Albo 10-16-2006 10:06 PM

Re: cascaded struct members.
 
BobR wrote:

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


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

--
Salu2

BobR 10-17-2006 01:53 AM

Re: cascaded struct members.
 

Julián Albo wrote in message <4534024e_3@x-privat.org>...
BobR wrote:

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


>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!
--
Bob <G> R
POVrookie




All times are GMT. The time now is 12:47 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.