C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

struct ... {} vs. class ... {}

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jens Müller
Guest





PostPosted: Tue May 30, 2006 2:35 pm    Post subject: struct ... {} vs. class ... {} Reply with quote



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





PostPosted: Wed May 31, 2006 4:20 pm    Post subject: Re: struct ... {} vs. class ... {} Reply with quote



=?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





PostPosted: Wed May 31, 2006 9:20 pm    Post subject: Re: struct ... {} vs. class ... {} Reply with quote



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





PostPosted: Sat Jun 03, 2006 7:11 pm    Post subject: Re: struct ... {} vs. class ... {} Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.