473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

class versus struct

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
13 4152
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
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
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
"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
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
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

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
* "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

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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being,...
11
by: Neil Zanella | last post by:
Hello, When an "std::map<int, int> foobar;" statement appears in a C++ program followed by a statement of the form "foobar;" where nothing has ever been inserted at position 123 into map foobar,...
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
53
by: SK | last post by:
Hi I appreciate all of the feedback I have recieved. I am enjoying working on my program and I hope that it can be appreciated. I have my program compiling now and I am continuing to work out...
12
by: Mark | last post by:
I always use the word "class" to describe a class that can be instantiated. However, I don't get the concept of "Type" or at least I don't know how to use the word properly. If I have a class...
1
by: Paul Aspinall | last post by:
Hi I've used both Struct and Classes. However, after reading a few articles lately, I'm starting to wonder why you would really use a Struct over a class. The only reason seems to be that...
7
by: Kenneth Siewers Møller | last post by:
Hi there I know this probably have been addressed before, but I have an object that should contain the following Guid id String fName String lName Could I use a struct for that? I have read...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
5
by: Ray | last post by:
Hi all, I am thinking of subclassing the standard string class so I can do something like: mystring str; .... str.toLower (); A quick search on this newsgroup has found messages by others
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.