Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Difference between struct and class

0 views
Skip to first unread message

Ook

unread,
Oct 3, 2005, 1:33:37 PM10/3/05
to
We have had a discussion on the differences between a class and a structure,
and no one is in agreement. As I understand it, a structure defaults to
public, a class defaults to private. There are issues about constructors
that I'm not clear on. I do know that I can take a simple project with a
class that has constuctors, destructors, accessors, and modifiers, change
the classes to structs, and it compiles and runs fine. Can some kind soul
outline the differences, or point me to a source that does so? TIA :)


mlimber

unread,
Oct 3, 2005, 1:48:32 PM10/3/05
to

Axter

unread,
Oct 3, 2005, 1:51:08 PM10/3/05
to

IAW C++ standard the only difference is the default public VS default
private.
That is it. Nothing else.

However, some programmers user a general rule in which they use struct
for POD types and class for complex types.

But that has nothing to do with the official C++ standard.

Ook

unread,
Oct 3, 2005, 4:44:00 PM10/3/05
to

So there are no difrerences with respect to default constructors, etc.? I
vaguely remember something related to constructors, I'll see if I can find
my lecture notes so I know what question to ask :P


mlimber

unread,
Oct 3, 2005, 4:57:05 PM10/3/05
to
Ook wrote:
> > See this FAQ:
> >
> > http://www.parashift.com/c++-faq-lite/classes-and-objects.html#faq-7.8
> >
> > Cheers! --M
> >
>
> So there are no difrerences with respect to default constructors, etc.?

Correct. Even initialization lists can work with classes, though
constructors are generally preferred:

class A { public: int i, j; };
A a1 = { 1, 42 }; // Ok
A a2; // Ok

struct B { int i, j; };
B b2 = { 1, 42 }; // Ok
B b2; // Ok

Cheers! --M

Gina

unread,
Oct 4, 2005, 8:00:41 AM10/4/05
to
I am new to c++ as well
... I do not know whether I got that right at all
but as far as I understand it is the difference between both:

1. a struct does not define behaviour ... it only defines the type
2. a class can have functions and assigns particular behaviour for an
object)

a class A has a function A::dosomething
so we can do
A.dosomething();

could that be what you were looking for ?

cheers,
Gina

"Axter" <goo...@axter.com> schrieb im Newsbeitrag
news:1128361868.6...@g44g2000cwa.googlegroups.com...

mlimber

unread,
Oct 4, 2005, 8:35:47 AM10/4/05
to
Gina wrote:
> I am new to c++ as well
> ... I do not know whether I got that right at all
> but as far as I understand it is the difference between both:
>
> 1. a struct does not define behaviour ... it only defines the type
> 2. a class can have functions and assigns particular behaviour for an
> object)
>
> a class A has a function A::dosomething
> so we can do
> A.dosomething();
>
> could that be what you were looking for ?
>
> cheers,
> Gina

Welcome, Gina. Your statement on the topic is incorrect. Check out the
FAQ:

http://www.parashift.com/c++-faq-lite/classes-and-objects.html#faq-7.8

This code will work fine:

#include <iostream>
using namespace std;

struct S
{
public:
void Foo()
{
i_ = 0xc0ffee;
cout << "S::Foo()" << endl;
}
private:
int i_;
};

class C
{
public:
Bar()
{
i_ = 42;
cout << "C::Bar()" << endl;
}
private:
int i_;
};

Cheers! --M

Karl Heinz Buchegger

unread,
Oct 4, 2005, 8:41:59 AM10/4/05
to
Gina wrote:
>
> I am new to c++ as well
> ... I do not know whether I got that right at all
> but as far as I understand it is the difference between both:
>
> 1. a struct does not define behaviour ... it only defines the type
> 2. a class can have functions and assigns particular behaviour for an
> object)

Maybe conceptually you are right and indeed this is how most programmers
treat that topic in practice.
But as C++ - the language - goes, it is incorrect. A 'struct' and a 'class'
differ only in 2 topics:
* members are per default public in a struct and private in a class
* the inheritance is per default public in a struct and private in a class

Other then that, there is no difference. At least none enforced by C++ - the language.
Any other differences in usage, as described by you above, may or may not be enforced
by your local design rules.

--
Karl Heinz Buchegger
kbuc...@gascad.at

Gina

unread,
Oct 4, 2005, 9:51:05 AM10/4/05
to
Hi & Thanks ....
for your answers .. and for the welcome ;-)

where I got what I stated here is from 'Visual C++ in 21 days' .pdf

herein is says:
... public ... private etc .... all exactly what has been said
and then ( free translation) :

'... difference between classes and structures is that structures don't
contain functionality they only contain data members
...
the main difference shows up in using them....structures are only a
container for data elements...'
... bit confusing ...
I have not read the link provided in the above post re topic.... which I
will !!
but
is it wrong then ... what's written in that book ??

Gina ?:-|


"Karl Heinz Buchegger" <kbuc...@gascad.at> schrieb im Newsbeitrag
news:43427897...@gascad.at...

mlimber

unread,
Oct 4, 2005, 10:14:47 AM10/4/05
to
Gina wrote:
> Hi & Thanks ....
> for your answers .. and for the welcome ;-)
>
> where I got what I stated here is from 'Visual C++ in 21 days' .pdf
>
> herein is says:
> ... public ... private etc .... all exactly what has been said
> and then ( free translation) :
>
> '... difference between classes and structures is that structures don't
> contain functionality they only contain data members
> ...
> the main difference shows up in using them....structures are only a
> container for data elements...'
> ... bit confusing ...
> I have not read the link provided in the above post re topic.... which I
> will !!
> but
> is it wrong then ... what's written in that book ??
>
> Gina ?:-|

The book is wrong in the sense that Karl detailed: the book is likely
describing common practice rather than what the language allows.

Cheers! --M

PS, It's considered bad manners to top-post in newsgroups. Put your
replies inline or at the bottom.

Karl Heinz Buchegger

unread,
Oct 4, 2005, 10:20:21 AM10/4/05
to
Gina wrote:
>
> Hi & Thanks ....
> for your answers .. and for the welcome ;-)
>
> where I got what I stated here is from 'Visual C++ in 21 days' .pdf
>
> herein is says:
> ... public ... private etc .... all exactly what has been said
> and then ( free translation) :
>
> '... difference between classes and structures is that structures don't
> contain functionality they only contain data members
> ...
> the main difference shows up in using them....structures are only a
> container for data elements...'
> ... bit confusing ...
> I have not read the link provided in the above post re topic.... which I
> will !!
> but
> is it wrong then ... what's written in that book ??
>
> Gina ?:-|

All books with something like ' ... in x days' need to take some shortcuts
and don't tell you everything. Well, in fact usually they tell you very little.
It is nearly impossible for a C++ book to cover every aspect of C++. Thus those
books are often incorrect (especially with the details) in wide areas.
Even if I am generous and interpret '21 days' as '21 lessons', this is still nowhere
near the truth. One cannot learn C++ in 21 lessons. It takes months to have some solid
understanding of the basics. Mastering C++ with the finer details takes years. So better
make a big bow around all books stating they can teach you x in y days.

Gina

unread,
Oct 4, 2005, 10:22:46 AM10/4/05
to
M.,
in the meantime I have read the article provided by your link ....

... and just wonder, how would a structure definition 'look' .... when
containing functionality ..... ???

books with incorrect statements !!! <deary me>
Gina

"mlimber" <mli...@gmail.com> schrieb im Newsbeitrag
news:1128435287.6...@g44g2000cwa.googlegroups.com...

Gina

unread,
Oct 4, 2005, 10:26:16 AM10/4/05
to
Hallo Karl-Heinz.

well .... I have to start somehwere .... and with some practical stuff ....
the book was for free ....

> All books with something like ' ... in x days' need to take some
shortcuts
> and don't tell you everything. Well, in fact usually they tell you very
little.
> It is nearly impossible for a C++ book to cover every aspect of C++. Thus
those
> books are often incorrect (especially with the details) in wide areas.

> Even if I am generous and interpret '21 days' as '21 lessons', this is
still nowhere
> near the truth. One cannot learn C++ in 21 lessons. It takes months to
have some solid
> understanding of the basics.

especially with c++ !!! your are totally right!!

Cheers,
Gina

mlimber

unread,
Oct 4, 2005, 10:30:58 AM10/4/05
to
Gina wrote:
> M.,
> in the meantime I have read the article provided by your link ....
>
> ... and just wonder, how would a structure definition 'look' .... when
> containing functionality ..... ???
>
> books with incorrect statements !!! <deary me>
> Gina

First, I will reiterate: don't top-post. Put your replies *BELOW* (or
inline with) the one you are quoting.

To answer your question, I'll refer you to my previous post in this
thread where I gave an example:

http://groups.google.com/group/comp.lang.c++/browse_frm/thread/6ebe925ed13dd021/3c64a9e4ffdabe3a#3c64a9e4ffdabe3a

Cheers! --M

Karl Heinz Buchegger

unread,
Oct 4, 2005, 10:46:04 AM10/4/05
to
Gina wrote:
>
> M.,
> in the meantime I have read the article provided by your link ....
>
> ... and just wonder, how would a structure definition 'look' .... when
> containing functionality ..... ???
>

just like a class.
A few seconds ago I wrote in one of my projects:

struct CStationDesc
{
CStationDesc( const CString& Key = "", short Id = 0, long Limit = 0, long Allocation = 0 )
: m_Key( Key ),
m_StationId( Id ),
m_Limit( Limit ),
m_Allocation( Allocation )
{}

CString m_Key;
short m_StationId;
long m_Limit;
long m_Allocation;
};

( Don't worry about CString. It is just another data type).
It is a structure I use for collecting data during a lengthy calculation process, storing
multiple such data sets in a vector, sorting them and presenting the results to the user.
I introduced that structure only to have a place where I can organize things. I could have
made it a class as well, with all members public or a class with all members private and
setter and getter functions. But I wanted to emphasize that this 'collection' is only needed
to have a nice place for data organization and no real intelligence is needed for them, that
is why I used a struct.
But in order to simplify life, I have given it a constructor (you can add any other
member function as well).

The constructor allows me to write

std::vector< CStationDesc > Results;

....

Results.push_back( CStationDesc( "Kalle", 123, Limit, Allocation ) );

instead of

CStationDesc Desc;

Desc.m_Key = "Kalle";
Desc.m_StationId = 123;
Desc.m_Limit = Limit;
Desc.m_Allocation = Allocation;

Results.push_back( Desc );

See. Much shorter.

mlimber

unread,
Oct 4, 2005, 10:47:47 AM10/4/05
to

Howard

unread,
Oct 4, 2005, 11:56:59 AM10/4/05
to

"Karl Heinz Buchegger" <kbuc...@gascad.at> wrote in message
news:43427897...@gascad.at...

Perhaps that bok is just old?

When I first started using C++ (about 1992, I think), the [Borland] compiler
I was using did indeed have those differences between class and struct which
Gina quotes from that book. A struct was strictly a C struct (POD), while a
class was a whole new entity, with member functions. The fact that some
early C++ compilers were written that way led some authors to present those
as specifications of the C++ language (as opposed to simply descriptions of
how some vendors implemented it). And since then, the common practice of
using struct for POD objects and class for more complex entities has helped
perpetuate what is now really a misconception about the language
specifications.

(I wouldn't be surprised if some older professors still teach that.
Certainly, I've seen that misconception here more than once.)

-Howard


Ook

unread,
Oct 4, 2005, 11:55:52 AM10/4/05
to

"Gina" <g...@freenet.de> wrote in message
news:dhu3b6$2qu$03$1...@news.t-online.com...

> M.,
> in the meantime I have read the article provided by your link ....
>
> ... and just wonder, how would a structure definition 'look' .... when
> containing functionality ..... ???
>
> books with incorrect statements !!! <deary me>
> Gina
>

I was taught in a c++ class at the local community college, and I have read
in articles posted here and there on the Internet, that a struct can't have
a parameterless constructor. However, the following code compiles and runs
just fine. If I understand what I'm doing, then I've been taught wrong :(

struct Zoot
{
public:
Zoot();
int size();
private:
int _size;
int* _data;
};

int Zoot::size(){return _size;}

// This should be my parameterless constructor
Zoot::Zoot()
{
_size = 5;
_data = new int[ _size ];
_data[0] = 123;
}

int main()
{
Zoot z;
cout << z.size() << endl;
return 0;
}


Gina

unread,
Oct 4, 2005, 12:28:01 PM10/4/05
to
Howard,

maybe ... it is old .... I was scrolling through it but couldn't find any
date

it is about vc++ and it is an online book *.pdf....
it states that the struct comes from old c ...

but thanks to everybody for help and clarification !!

Gina

"Howard" <ali...@hotmail.com> schrieb im Newsbeitrag
news:fDx0f.369029$5N3.1...@bgtnsc05-news.ops.worldnet.att.net...

Howard

unread,
Oct 4, 2005, 12:31:18 PM10/4/05
to

"Ook" <nousenetspam at dead ice dot us> wrote in message
news:CsCdnWEU4Mk...@giganews.com...
>

> I was taught in a c++ class at the local community college, and I have
> read in articles posted here and there on the Internet, that a struct
> can't have a parameterless constructor. However, the following code
> compiles and runs just fine. If I understand what I'm doing, then I've
> been taught wrong :(
>

That's a C# specification (in .NET), if I recall correctly. It most
certainly is wrong in regards to standard C++.

-Howard


Gina

unread,
Oct 4, 2005, 12:32:36 PM10/4/05
to
thanks, mlimber,

for illustrating it for me and other newbies

Gina
( I hope I am answering 'inline' now ?)

"mlimber" <mli...@gmail.com> schrieb im Newsbeitrag

news:1128437267....@o13g2000cwo.googlegroups.com...

Default User

unread,
Oct 4, 2005, 4:47:36 PM10/4/05
to
Howard wrote:



> When I first started using C++ (about 1992, I think), the [Borland]
> compiler I was using did indeed have those differences between class
> and struct which Gina quotes from that book. A struct was strictly a
> C struct (POD), while a class was a whole new entity, with member
> functions.

That wasn't the case for Turbo C++ 3.0, circa 1992.


Brian

Fabio Fracassi

unread,
Oct 5, 2005, 4:21:28 AM10/5/05
to
Gina wrote:

> thanks, mlimber,
>
> for illustrating it for me and other newbies
>
> Gina
> ( I hope I am answering 'inline' now ?)

No, you are still top-posting.
http://en.wikipedia.org/wiki/Top-posting

HTH

Fabio

0 new messages