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.
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
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
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...
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
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
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...
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.
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.
... 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...
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
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:
Cheers! --M
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.
That should have been:
http://groups.google.com/group/comp.lang.c++/msg/99dcd701dbfd1e56
Cheers! --M
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
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;
}
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...
> 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
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...
> 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
> 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