class versus struct

O

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

Victor Bazarov

Ook said:
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
 
O

Ook

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.
 
M

Matthias Kaeppler

Victor said:
<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.
 
J

John Carson

Ook said:
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.
 
V

Victor Bazarov

Matthias said:
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
 
V

Victor Bazarov

Ook said:
[..] 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
 
A

Alf P. Steinbach

* "Ook said:
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'.
 
M

ma740988

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
 
V

Victor Bazarov

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
 
M

ma740988

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
 
V

Victor Bazarov

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...
 
G

Greg

Ook said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top