Topic: class a : some_struct" illegal?


Author: Mark Williams <Mark@streetly.demon.co.uk>
Date: 1995/06/18
Raw View
In article <reinder-1206951712160001@neuretc.biol.ruu.nl>, Reinder Verlinde writes:

>
> I have some code which does:
>
> class standardgetfile : protected StandardFileReply
> {
>
> };
>
> where StandardFileReply is a struct. This compiles fine under my C++ compiler
> (Symantec C++ for Macintosh). A beta tester complained that his C++ compiler
> (MetroWerks 6) does not allow it. (invalid base class or something like it)
> He also writes:
>
> >I have checked in the ARM whether or not your approach is correct.
> >It seems not. According the to the ARM, a base class specifier must always be
> >composed of a complete-class-name, which is never equivalent to a
> >qualified-type-name, which StandardFileReply is. Both are subcategories of
> >simple-type-name. In other words, under an ANSI C++ compiler, this class
> >definition is bound to break.
>
> The natural question thus is: who is right? I always thought that structure
> and class are almost synonyms. I do trust my beta testers, but also know that
> the ARM is a moving target. Please answer by E-mail, too.
>
> Reinder Verlinde

the only difference between class and struct is the default access (ie private/public) of members
and bases.

it is legal to derive a class from a struct or a struct from a class.

the following is also legal:

struct C;

void f(C*);

class C { ... };

C c1;
class C c2;
struct C c3;

void g()
{
 f(&c1);
 f(&c2);
 f(&c3);
}

ie class and struct are interchangeable.

BUT - there is a bug with CW precompiled headers:
If a struct is declared in a precompiled header which was precompiled using the C compiler, you cant
derive anything from it.

----------------------------------------
Mark Williams<Mark@streetly.demon.co.uk>





Author: reinder@neuretp.biol.ruu.nl (Reinder Verlinde)
Date: 1995/06/12
Raw View
I have some code which does:

class standardgetfile : protected StandardFileReply
{

};

where StandardFileReply is a struct. This compiles fine under my C++ compiler
(Symantec C++ for Macintosh). A beta tester complained that his C++ compiler
(MetroWerks 6) does not allow it. (invalid base class or something like it)
He also writes:

>I have checked in the ARM whether or not your approach is correct.
>It seems not. According the to the ARM, a base class specifier must always be
>composed of a complete-class-name, which is never equivalent to a
>qualified-type-name, which StandardFileReply is. Both are subcategories of
>simple-type-name. In other words, under an ANSI C++ compiler, this class
>definition is bound to break.

The natural question thus is: who is right? I always thought that structure
and class are almost synonyms. I do trust my beta testers, but also know that
the ARM is a moving target. Please answer by E-mail, too.

Reinder Verlinde





Author: Todd Short <tshort@baynetworks.com>
Date: 1995/06/12
Raw View
reinder@neuretp.biol.ruu.nl (Reinder Verlinde) wrote:
>I have some code which does:
>
>class standardgetfile : protected StandardFileReply
>{
>
>};
>
>where StandardFileReply is a struct. This compiles fine under my C++ compiler
>(Symantec C++ for Macintosh). A beta tester complained that his C++ compiler
>(MetroWerks 6) does not allow it. (invalid base class or something like it)
>He also writes:
>
>The natural question thus is: who is right? I always thought that structure
>and class are almost synonyms. I do trust my beta testers, but also know that
>the ARM is a moving target. Please answer by E-mail, too.
>

The differences of the class/struct:

1. class default protection is private
   struct default protection is public
2. structs can be "plain old structs" where there is no vtbl information,
   and the sizeof the struct is the sum of the struct's contents (subject
   to packing rules), such that doing C operations (such as read() and
   write in Unix) do not break. This is from DEC++ (somewhere in there).
3. class can be used to refer to any data structure (i.e. in templates)

Here's another way to think about it:
If short and int are both 16-bit, they are still different types, even
though they are practically the same!

struct maintains C backwards compatibility, but doesn't do anything
less than class. since things are declared differently, I'd say that
one would have to use a struct to inherit from a struct, and a class
to inherit from a class.

--
-Todd Short
// NOW:   tshort@wellfleet.com
// LATER: tshort@baynetworks.com
// "One if by land, two if by sea, three if by the internet".






Author: reinder@neuretp.biol.ruu.nl (Reinder Verlinde)
Date: 1995/06/12
Raw View
In article <3rhume$29h@paperboy.wellfleet.com>, Todd Short
<tshort@baynetworks.com> wrote:

> 2. structs can be "plain old structs" where there is no vtbl information,
>    and the sizeof the struct is the sum of the struct's contents (subject
>    to packing rules), such that doing C operations (such as read() and
>    write in Unix) do not break. This is from DEC++ (somewhere in there).
> 3. class can be used to refer to any data structure (i.e. in templates)
>
> Here's another way to think about it:
> If short and int are both 16-bit, they are still different types, even
> though they are practically the same!
>
> struct maintains C backwards compatibility, but doesn't do anything
> less than class. since things are declared differently, I'd say that
> one would have to use a struct to inherit from a struct, and a class
> to inherit from a class.
>
I think point 3. above does imply that one should be able to do

class S : a_struct {};

It also fits in with point 2. above, in that S will have a vtbl, but
a_struct will not. Of course, one will have to do some pointer magic
when casting a S to a 'a_struct', but that would not be different from
the situation where one would have

class S : a_class {};

Reinder Verlinde





Author: woody@alumni.cco.caltech.edu (William Edward Woody)
Date: 1995/06/12
Raw View
<tshort@baynetworks.com> wrote:
> struct maintains C backwards compatibility, but doesn't do anything
> less than class. since things are declared differently, I'd say that
> one would have to use a struct to inherit from a struct, and a class
> to inherit from a class.

No cigar, but thank you for playing.

In C++, the declaration

        struct FOO {
                ...
        };

is exactly equal to:

        class FOO {
            public:
                ....
        };

and so the struct declaration is simply a shortcut for a class with all
public declarations. It is not a different thing.

And since a struct is basically a shorthand for a class, classes should
be able to inherent from structs. (Can structs inherit from classes?
I think so, but I could be wrong.)

I suspect the problem is that the precompiled headers were compiled with
the C compiler (instead of being precompiled in C++ mode), and it's causing
the compiler problems.

(It could be a problem because the name space in C is a little different
than in C++, and it's possible that the Metrowerks compiler isn't coping
with that fact real well.)

                                                        - Bill

--
William Edward Woody  |  e-mail: woody@alumni.cco.caltech.edu
In Phase Consulting   |  WWW:    http://www.alumni.caltech.edu/~woody
337 W. California #4  |  Fax:    (818) 502-1467
Glendale, CA 91203    |  ICBM:   N:34.4' W:118.15'





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/06/13
Raw View
Todd Short <tshort@baynetworks.com> writes:

>reinder@neuretp.biol.ruu.nl (Reinder Verlinde) wrote:
>>I have some code which does:
>>
>>class standardgetfile : protected StandardFileReply
>>{
>>
>>};
>>
>>where StandardFileReply is a struct. This compiles fine under my C++ compiler
>>(Symantec C++ for Macintosh). A beta tester complained that his C++ compiler
>>(MetroWerks 6) does not allow it. (invalid base class or something like it)
>>
>>The natural question thus is: who is right? I always thought that structure
>>and class are almost synonyms. I do trust my beta testers, but also know that
>>the ARM is a moving target. Please answer by E-mail, too.
>>

>The differences of the class/struct:

>1. class default protection is private
>   struct default protection is public
>2. structs can be "plain old structs" where there is no vtbl information,
>   and the sizeof the struct is the sum of the struct's contents (subject
>   to packing rules), such that doing C operations (such as read() and
>   write in Unix) do not break. This is from DEC++ (somewhere in there).
>3. class can be used to refer to any data structure (i.e. in templates)


You have managed to mix up several unrelated things here.

#1 is correct. In C++, "class" and "struct" types are the same. The
ONLY difference between a type defined as a struct and one defined
as a class is the default protection of members and base classes
in the absence of any access modifier. That is,
 struct B { int k; };
 struct D : B { int i; };

and
 class B { public: int k; };
 class D : public B { public: int i; };
mean exactly the same thing. There is no difference. In fact, you
can refer to a type using the keyword "class" in one place and
"struct" in another:
 struct C; // forward declaration
 C* foo();
 class C { ... }; // and now define it


Consequently, it is perfectly valid to derive a class from a
struct, or a struct from a class. The two keywords refer to
the same thing. If the original poster wrote something like
 struct StandardFileReply { ... };
 class standardgetfile : protected StandardFileReply { ... };
that code is valid if the contents of the two classes (structs)
are valid.


#2: A special kind of struture type is known as a POD-struct or
POD-union, where "POD" mean "Plain Old Data". Without going into
the details, it is a concept separate from the equivalence of the
"struct" and "class" keywords. A struct or class is or is not a
POD-struct depending only on its definition, not on the use of
the "struct" or "class" keyword.


#3: In the syntax of of a template definition, the keyword "class"
introduces a name which stands for a type. This is special use of
the keyword "class" which independent of the data structure
known as a class. (Just as the keyword "static" has several
unrelated meanings.)

The C++ draft standard had added a new keyword "typename" which is
needed for another purpose where "class" cannot be used. You may
use "typename" instead of "class" to introduce a type parameter
to a template if you wish.
--
Steve Clamage, stephen.clamage@eng.sun.com