| View previous topic :: View next topic |
| Author |
Message |
Jens Müller Guest
|
Posted: Tue May 30, 2006 2:35 pm Post subject: struct ... {} vs. class ... {} |
|
|
In "Modern C++ Design" by Andrei Alexandrescu, § 2.9, there are the
following definitions:
class NullType {};
struct EmptyType {};
Is there any special reason why he uses class in one case and struct in
the other?
Since both don't have any data-member at all, aren't those defs
identical (apart from the name)?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Tomás Guest
|
Posted: Wed May 31, 2006 4:20 pm Post subject: Re: struct ... {} vs. class ... {} |
|
|
=?ISO-8859-15?Q?Jens_M=FCller?= posted:
| Quote: | class NullType {};
struct EmptyType {};
|
"class" and "struct" are identical except for one thing: Default Access
Specifier.
"class" is private by default.
"struct" is public by default.
There's one other difference, you can use struct names as tags:
struct SomeStruct FunReturnByValue(void);
But that's never done in C++ so it's no big deal.
To demonstrate, the following two are exactly equivalent:
struct Monkey : Ape {
int k;
};
class Monkey : public Ape {
public:
int k;
};
The following two are exactly equivalent also:
struct Monkey : private Ape {
private:
int k;
};
class Monkey : Ape {
int k;
};
-Tomás
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Jens Müller Guest
|
Posted: Wed May 31, 2006 9:20 pm Post subject: Re: struct ... {} vs. class ... {} |
|
|
Tomás schrieb:
| Quote: | =?ISO-8859-15?Q?Jens_M=FCller?= posted:
class NullType {};
struct EmptyType {};
"class" and "struct" are identical except for one thing: Default Access
Specifier.
"class" is private by default.
"struct" is public by default.
|
So these two are identical, I got that.
I just thought there might be some reason behind the different
declarations, like an idiom or code conventions.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Sat Jun 03, 2006 7:11 pm Post subject: Re: struct ... {} vs. class ... {} |
|
|
Tomás wrote:
| Quote: |
There's one other difference, you can use struct names as tags:
struct SomeStruct FunReturnByValue(void);
|
Pardon? I'm not sure what you are trying to say here
but
class SomeStruct FunReturnByValue();
works just as well (and it doesn't much matter if
SomeStruct was previous defined or not nor if it
were defined whether it was defined as a struct
or class.
Other than the default access, the only different
is that the keyword "class" can be used to declare
template variables (synonymous with typename) and
struct can not.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|