Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > cascaded struct members.

Reply
Thread Tools

cascaded struct members.

 
 
sunny
Guest
Posts: n/a
 
      10-16-2006
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.

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      10-16-2006
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-16-2006
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


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      10-16-2006
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?
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      10-16-2006

Ron Natalie wrote in message
<4533c2bf$0$5922$(E-Mail Removed)>. ..
>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


 
Reply With Quote
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      10-16-2006
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
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      10-17-2006

Julián Albo wrote in message <(E-Mail Removed)>...
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cascaded menus and multi-select CheckBoxList dbuchanan ASP .Net 6 06-11-2008 05:00 AM
GENERATE - cascaded ALuPin@web.de VHDL 2 04-28-2008 03:27 PM
Cascaded Macros -- Back-Up Frank C Programming 1 11-22-2007 03:26 PM
Speedtouch 716v5 - can it be cascaded from cable ? Jim UK VOIP 6 05-01-2006 10:28 PM
C Preprocessor - Cascaded Macros Michael Blackney C Programming 1 09-09-2004 11:43 PM



Advertisments