By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,903 Members | 1,090 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,903 IT Pros & Developers. It's quick & easy.

class versus struct

P: n/a
Ook
I have been told that the only difference between a class and a struct is
the default interface - private for class, public for struct. My instructor
made this statement:

Actually there are only 2 differences between classes and structs:
1) The default interface for a class is private whereas it is publie for a
struct.
2) A struct supports array-type initialization syntax in addition to
constructors whereas a class requires constructors only.

And give the following code example:

// Works
class Point { int x, y; };
Point p1 = {4, 6};

// Compile error: 'p1' : non-aggregates cannot be initialized with
initializer list
struct Point { int x, y; };
Point p1 = {4, 6};

Is this correct, and these are the only 2 differences?
Nov 1 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Ook wrote:
I have been told that the only difference between a class and a struct is
the default interface - private for class, public for struct. My instructor
made this statement:

Actually there are only 2 differences between classes and structs:
1) The default interface for a class is private whereas it is publie for a
struct.
2) A struct supports array-type initialization syntax in addition to
constructors whereas a class requires constructors only.

And give the following code example:

// Works
class Point { int x, y; };
Point p1 = {4, 6};

// Compile error: 'p1' : non-aggregates cannot be initialized with
initializer list
struct Point { int x, y; };
Point p1 = {4, 6};

Is this correct, and these are the only 2 differences?


No. Another difference is that the keyword 'class' can be used in
a template type argument declaration as an alternative to 'typename'
and 'struct' cannot.

Isn't all this described in the FAQ? Why do we keep talking about it
when it's all has be discussed countless number of times? Don't you
have access to 'groups.google.com'?

V
Nov 1 '05 #2

P: n/a
Victor Bazarov wrote:
<snip>

No. Another difference is that the keyword 'class' can be used in
a template type argument declaration as an alternative to 'typename'
and 'struct' cannot.

Isn't all this described in the FAQ? Why do we keep talking about it
when it's all has be discussed countless number of times? Don't you
have access to 'groups.google.com'?


On top of that, inheritance for a struct is implicitly public, while
it's private (or protected?) for a class.
Nov 1 '05 #3

P: n/a
Ook
>>
Is this correct, and these are the only 2 differences?


No. Another difference is that the keyword 'class' can be used in
a template type argument declaration as an alternative to 'typename'
and 'struct' cannot.

Isn't all this described in the FAQ? Why do we keep talking about it
when it's all has be discussed countless number of times? Don't you
have access to 'groups.google.com'?

V


Thank you for your response. We keep talking about it because not everyone
knows there is an FAQ, or they can't find the answer to their question in
the FAQ, or they can't google the answer.
Nov 1 '05 #4

P: n/a
"Ook" <nousenetspam at dead ice dot us> wrote in message
news:Rr********************@giganews.com
I have been told that the only difference between a class and a
struct is the default interface - private for class, public for
struct. My instructor made this statement:

Actually there are only 2 differences between classes and structs:
1) The default interface for a class is private whereas it is publie
for a struct.
2) A struct supports array-type initialization syntax in addition to
constructors whereas a class requires constructors only.
2) is wrong. The correct statement is that array-type initialization syntax
is only possible when all data members are public. This is the case by
default with a struct, but not with a class.

And give the following code example:

// Works
class Point { int x, y; };
Point p1 = {4, 6};

// Compile error: 'p1' : non-aggregates cannot be initialized with
initializer list
struct Point { int x, y; };
Point p1 = {4, 6};


You have these confused. It is the class that fails, not the struct. And, as
noted above, the only reason why the class fails is because its members are
private by default. Change it to:

class Point { public: int x, y; };

int main()
{
Point p1 = {4, 6};
}

and it will compile.
--
John Carson

Nov 1 '05 #5

P: n/a
Matthias Kaeppler wrote:
Victor Bazarov wrote:
<snip>

No. Another difference is that the keyword 'class' can be used in
a template type argument declaration as an alternative to 'typename'
and 'struct' cannot.

Isn't all this described in the FAQ? Why do we keep talking about it
when it's all has be discussed countless number of times? Don't you
have access to 'groups.google.com'?

On top of that, inheritance for a struct is implicitly public, while
it's private (or protected?) for a class.


It's private for a class, but you can sort of derive this from the member
access rule: base class members are inherited (become _members_) for the
purpose of name lookup, for example. And the class _name_ is its member,
however strange this might sound.

V
Nov 1 '05 #6

P: n/a
Ook wrote:
[..] We keep talking about it because not everyone


Do you mean yourself by "not everyone", or do we keep talking about
it for the benefit of some unknown others? If you do not know where
the FAQ is, or need help finding something in it, just ask.

V
Nov 1 '05 #7

P: n/a

Ook wrote:
... not everyone
knows there is an FAQ, or they can't find the answer to their question in
the FAQ, or they can't google the answer.


It's at
http://www.parashift.com/c++-faq-lite/

which was the first hit I got googling for "c++ faq"

Gavin Deane

Nov 1 '05 #8

P: n/a
* "Ook" <nousenetspam at dead ice dot us>:
I have been told that the only difference between a class and a struct is
the default interface - private for class, public for struct. My instructor
made this statement:

Actually there are only 2 differences between classes and structs:
1) The default interface for a class is private whereas it is publie for a
struct.
Yes.
2) A struct supports array-type initialization syntax in addition to
constructors whereas a class requires constructors only.
No.

And give the following code example:

// Works
class Point { int x, y; };
Point p1 = {4, 6};

// Compile error: 'p1' : non-aggregates cannot be initialized with
initializer list
struct Point { int x, y; };
Point p1 = {4, 6};

Is this correct,
The example is correct. HOwever, consider

class Point{ public: int x, y; };

This is the equivalent of the struct declaration.

And here you can use a brace-enclosed initializer list.

and these are the only 2 differences?


In addition to the one difference you already have, in a template
parameter list you can use 'class' as a synonym for 'typename'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 1 '05 #9

P: n/a

Greetings Vic,

|| And the class _name_ is its member, however strange this might
sound.
Just so I understand this (must have perused that line three times).
Put another way: For name lookup purpose, the 'base' class _name_ is
the name in the derived? Perhaps I'm missing something with the line
'is its member'.

Thanks

Nov 1 '05 #10

P: n/a
ma******@gmail.com wrote:
Greetings Vic,

|| And the class _name_ is its member, however strange this might
sound.
Just so I understand this (must have perused that line three times).
Put another way: For name lookup purpose, the 'base' class _name_ is
the name in the derived? Perhaps I'm missing something with the line
'is its member'.


class A {};

inside the class 'A's scope, "A" is a _member_. When you derive from
a class, all its members are inherited (unless they are hidden). So,
when you write

class B : A {};

inside the class 'B's scope, "A" is a _member_, just like "B" is.

V
Nov 1 '05 #11

P: n/a

I see. I tend to struggle with the terminology from time to time. For
instance:
|| just like "B" is.

So B is a member of itself :). The wording at times seems strange to
me.

Oh well!! Thanks

Nov 1 '05 #12

P: n/a
ma******@gmail.com wrote:
I see. I tend to struggle with the terminology from time to time. For
instance:
|| just like "B" is.

So B is a member of itself :). The wording at times seems strange to
me.


You put it that way, not I...
Nov 2 '05 #13

P: n/a
Ook wrote:
I have been told that the only difference between a class and a struct is
the default interface - private for class, public for struct. My instructor
made this statement:

Actually there are only 2 differences between classes and structs:
1) The default interface for a class is private whereas it is publie for a
struct.
2) A struct supports array-type initialization syntax in addition to
constructors whereas a class requires constructors only.

And give the following code example:

// Works
class Point { int x, y; };
Point p1 = {4, 6};

// Compile error: 'p1' : non-aggregates cannot be initialized with
initializer list
struct Point { int x, y; };
Point p1 = {4, 6};

Is this correct, and these are the only 2 differences?


No, it is not correct. The default access level is the only difference
between a class and a struct declaration. And it seems unlikely that
there is a C++ compiler out there that behaves otherwise.

In other words, I doubt that there is a C++ compiler that would fail to
compile this program:

int main()
{
class PointClass { public: int x, y; };
PointClass p1 = {4, 6};

struct PointStruct { int x, y; };
PointStruct p2 = {4, 6};
}

And even if such a C++ compiler does exist, it would be in error.

Greg

Nov 2 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.