Somebody was asking about C++ a while back - I came across this handy FAQ today, which contains some pretty advanced stuff, so it's probably going to be useful for most people:
http://www.parashift.com/c++-faq-lite/
Enjoy
Munch
C++ help
Moderator: Moderators
- PauloMorfeo
- Posts: 2004
- Joined: 15 Dec 2004, 20:53
http://www.parashift.com/c++-faq-lite/c ... jects.html
A struck works like the other simple variables. If you pass it into a method/function, it sends, by default, a copy of it.
A Class works like arrays. When you're using it, you're using it's pointer and, as such, if you pass it into a method/function, it always sends it's pointer.
Even though it is true that they are pretty similar but people have diferent feelings for it (probably because of all that propaganda about Object Oriented programming).
If i'm not mistaken, that information is incorrect (the very first paragraph i read).[7.8] What's the difference between the keywords struct and class?
The members and base classes of a struct are public by default, while in class, they default to private. ...
struct and class are otherwise functionally equivalent.
... Emotionally, most developers make a strong distinction between a class and a struct. A struct simply feels like ... A class feels like ... but otherwise you should probably use the class keyword.
A struck works like the other simple variables. If you pass it into a method/function, it sends, by default, a copy of it.
A Class works like arrays. When you're using it, you're using it's pointer and, as such, if you pass it into a method/function, it always sends it's pointer.
Even though it is true that they are pretty similar but people have diferent feelings for it (probably because of all that propaganda about Object Oriented programming).
- sp2danny72
- Posts: 60
- Joined: 09 Jan 2005, 04:52
No, the information in the FAQ is correct.
Both structs and classes can contain both variables and methods.
When you are dealing with a class, there is NOT an implied pointer,
thats Objective-Pascal you're thinking of.
Hoever, most programmers automaticly uses a struct for a POD
(Plain Ole' Data) and class for a object-oriented object. Thus,
readabillity of code increases if you use this convention.
Having some functions as a memeber of a struct can be
convienient, even if its a POD (ie no VTBL)
Example:
'Date' is still a POD, (ie no overhead with VTBL) and
still only uses up 12 bytes. Writing:
is exacly the same thing.
If you pass a Date you pass the whole Date, not a pointer.
Both structs and classes can contain both variables and methods.
When you are dealing with a class, there is NOT an implied pointer,
thats Objective-Pascal you're thinking of.
Hoever, most programmers automaticly uses a struct for a POD
(Plain Ole' Data) and class for a object-oriented object. Thus,
readabillity of code increases if you use this convention.
Having some functions as a memeber of a struct can be
convienient, even if its a POD (ie no VTBL)
Example:
Code: Select all
struct Date {
int y,m,d;
std::string Print() const;
};
still only uses up 12 bytes. Writing:
Code: Select all
class Date {
public:
int y,m,d;
std::string Print() const;
};
If you pass a Date you pass the whole Date, not a pointer.
- PauloMorfeo
- Posts: 2004
- Joined: 15 Dec 2004, 20:53
-
- Posts: 1
- Joined: 14 Nov 2005, 15:25
Hi I'm new here. I saw this thread and thought I might be able to clear this up as I was just researching the same subject.
Bjarne says:
struct s { ...
is shorthand for
class s { public: ...
I think this clears it up nicely and answers any questions you might have about inheritance and such (i.e. structs can do everything classes can).
And, if you're going to pass a class to a function that modifies it, use a pointer or reference, and if it doesn't modify it, use a const qualifier.
Bjarne says:
struct s { ...
is shorthand for
class s { public: ...
I think this clears it up nicely and answers any questions you might have about inheritance and such (i.e. structs can do everything classes can).
And, if you're going to pass a class to a function that modifies it, use a pointer or reference, and if it doesn't modify it, use a const qualifier.
- [K.B.] Napalm Cobra
- Posts: 1222
- Joined: 16 Aug 2004, 06:15
- sp2danny72
- Posts: 60
- Joined: 09 Jan 2005, 04:52
- sp2danny72
- Posts: 60
- Joined: 09 Jan 2005, 04:52
C++ started as "C with classes"
It was basicly implemented as a preprocessor that
generated C code. It used 'class' as a flag for including a VTBL.
That difference between class and struct was later removed,
and both 'class' and 'struct' where kept for backwards compabillity.
Google 'bjarne stroustrup', click on the first link, click on "C++ glossary" and
scroll down to 'struct'. It says:
struct - class with members public by default. Most often used for data structures without member functions or class invariants, as in C-style programming. TC++PL 5.7, 10.2.8, D&E 3.5.1.
It was basicly implemented as a preprocessor that
generated C code. It used 'class' as a flag for including a VTBL.
That difference between class and struct was later removed,
and both 'class' and 'struct' where kept for backwards compabillity.
Google 'bjarne stroustrup', click on the first link, click on "C++ glossary" and
scroll down to 'struct'. It says:
struct - class with members public by default. Most often used for data structures without member functions or class invariants, as in C-style programming. TC++PL 5.7, 10.2.8, D&E 3.5.1.