C++ help

C++ help

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

Post Reply
User avatar
munch
Posts: 311
Joined: 26 May 2005, 20:00

C++ help

Post by munch »

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
User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

Post by PauloMorfeo »

http://www.parashift.com/c++-faq-lite/c ... jects.html
[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.
If i'm not mistaken, that information is incorrect (the very first paragraph i read).
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).
User avatar
Dragon45
Posts: 2883
Joined: 16 Aug 2004, 04:36

Post by Dragon45 »

A struct can contain variables. A class can contain variables AND functions. That's the big difference.
User avatar
sp2danny72
Posts: 60
Joined: 09 Jan 2005, 04:52

Post by sp2danny72 »

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:

Code: Select all

struct Date {
  int y,m,d;
  std::string Print() const;
};
'Date' is still a POD, (ie no overhead with VTBL) and
still only uses up 12 bytes. Writing:

Code: Select all

class Date {
  public:
    int y,m,d;
    std::string Print() const;
};
is exacly the same thing.
If you pass a Date you pass the whole Date, not a pointer.
User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

Post by PauloMorfeo »

sp2danny72 wrote:...
When you are dealing with a class, there is NOT an implied pointer,
thats Objective-Pascal you're thinking of.
...
No, that was probably me thinking about C#. C# works like that.

Anyway, don't the classes suport stuff like inheritance and polimorfism while structs not?
Absolution
Posts: 1
Joined: 14 Nov 2005, 15:25

Post by Absolution »

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.
User avatar
[K.B.] Napalm Cobra
Posts: 1222
Joined: 16 Aug 2004, 06:15

Post by [K.B.] Napalm Cobra »

I'm pretty sure struct != class {public.
User avatar
sp2danny72
Posts: 60
Joined: 09 Jan 2005, 04:52

Post by sp2danny72 »

Napalm, give up, I think Bjarne knows his own language...
User avatar
[K.B.] Napalm Cobra
Posts: 1222
Joined: 16 Aug 2004, 06:15

Post by [K.B.] Napalm Cobra »

Then why add classes at all?
User avatar
sp2danny72
Posts: 60
Joined: 09 Jan 2005, 04:52

Post by sp2danny72 »

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.
Post Reply

Return to “Engine”