This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Anonymous structs in C++
- To: brent at rcfile dot org
- Subject: Re: Anonymous structs in C++
- From: Francois Gouget <fgouget at free dot fr>
- Date: Wed, 4 Oct 2000 13:06:34 -0700 (PDT)
- cc: gcc at gcc dot gnu dot org
- Reply-To: Francois Gouget <fgouget at free dot fr>
Hi,
On Wed, 4 Oct 2000 brent@rcfile.org wrote:
> On 03 Oct 2000 at 17:46 (-0700), Francois Gouget wrote:
[...]
> | Actually there's still a small bug in the anonymous struct support in
> | g++. I thought I'd mention it here in case it went by unnoticed (didn't
> | see it in the archives). With the program below g++ says:
> |
> | $ g++ -c foo.cpp
> | foo.cpp: In function `int main()':
> | foo.cpp:28: `struct anon_struct2' has no member named `a'
> | foo.cpp:29: `struct anon_struct2' has no member named `b'
>
> it doesn't AFAICT. the 'struct nested;' line in anon_struct2 is
> just a declaration of the nested struct, not a definition. are
> you sure this code _should_ work. if so, could you please refer
> me to it's documented behavior.
Well, I was wrong. I reported this as a bug (c++/597) and it
turns out there is no bug. See:
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=597&database=gcc
In C you can do:
struct point {
int x,y;
};
struct rectangle {
struct point;
int w,h;
};
And here you are, instant inheritance. You can then use x, y, w, and
h to manipulate your rectangles. Because 'struct point;' declares an
unnamed field of type 'struct point'. I find this kind of neat.
But in C++, the exact same code behaves differently and as you
pointed out 'struct point;' is just a declaration of the 'point' struct.
But, of course, if you need inheritance in C++ you have better ways of
getting it.
This prompted me to check whether the VC++ complies to the standard
and it even does (I should have checked before).
I hate it when the same code behaves differently in C and C++. I
recently discovered another difference related to the size of a
character litteral: sizeof('c'). Try it for yourself, the C behavior
makes no sense but it's in the standard anyway :-/.
--
Francois Gouget fgouget@free.fr http://fgouget.free.fr/
Advice is what we ask for when we already know the answer but wish we didn't
-- Eric Jong