www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - design question - class vs. struct

reply clayasaurus <clayasaurus yahoo.com> writes:
Hi, recently I've been trying to design a 3d game engine, and I was 
thinking about using a bunch of different classes that work together to 
handle various different things I want to do (input, window, player, 
box). However, I've been having some trouble with classes in D (read: 
"The delete that never ends the program" posting), and was wondering if 
I should just use structs instead, like so...

struct playerData {...}
playerInitialize(playerData pd)
playerMouseLook(playerData pd, etc..)

The question I have is this. Why do I need to use the new operator to 
create a new class (cClass class = new cClass) and not to create a new 
stuct?

When should I use a class, and when should I use structs with functions 
that modify that struct?

and is it ok to imbed a class within a class like

cPlayer
{
    cVector vector;
}
?

I am a bit confused, and would appreciate help. thanks.
May 24 2004
next sibling parent Norbert Nemec <Norbert.Nemec gmx.de> writes:
class objects are generally stored on the heap. A class variable actually
always is a reference to the object data on the heap.

struct are stored inline, i.e. directly on the stack, directly within the
object data or with an enveloping struct that contains the inner struct.
You may, of course allocate a struct on the heap, but then you have to
handle it using a pointer.

In general, you can view structs as "lightweight classes" - there are no
inheritance, no interfaces and no constructor/destructor. For small data
sets, structs perform better, since they have less overhead (allocation,
dereferenciation) Once the data gets bigger, the copying of the data may
weigh in heavier. If you need polymorphism, structs are no option anyway.
Last argument in favor of structs may be their value-semantics, which would
probably be what you expect of stuff like you cVector.



clayasaurus wrote:

 Hi, recently I've been trying to design a 3d game engine, and I was
 thinking about using a bunch of different classes that work together to
 handle various different things I want to do (input, window, player,
 box). However, I've been having some trouble with classes in D (read:
 "The delete that never ends the program" posting), and was wondering if
 I should just use structs instead, like so...
 
 struct playerData {...}
 playerInitialize(playerData pd)
 playerMouseLook(playerData pd, etc..)
 
 The question I have is this. Why do I need to use the new operator to
 create a new class (cClass class = new cClass) and not to create a new
 stuct?
 
 When should I use a class, and when should I use structs with functions
 that modify that struct?
 
 and is it ok to imbed a class within a class like
 
 cPlayer
 {
     cVector vector;
 }
 ?
 
 I am a bit confused, and would appreciate help. thanks.
May 24 2004
prev sibling next sibling parent Arcane Jill <Arcane_member pathlink.com> writes:
In article <c8u159$132i$1 digitaldaemon.com>, clayasaurus says...

Norbert answered this very well, but I should just like to add:

re:
    (cClass class = new cClass)
and
    cVector vector;
From the D style guide: Class, Struct, Union, Enum names are capitalized.
	class Foo;
	class FooAndBar;
Hungarian Notation Just say no.
May 25 2004
prev sibling parent Ilya Minkov <minkov cs.tum.edu> writes:
clayasaurus schrieb:

 struct playerData {...}
 playerInitialize(playerData pd)
 playerMouseLook(playerData pd, etc..)
Better so: struct PlayerData{ initialize(); mouseLook(etc..); ... } then (if you don't forget to define the functions) you can do: PlayerData player; player.initialize(); ... just as you would in C++. All non-static functions get a hidden parameter "PlayerData * this". :)
 When should I use a class, and when should I use structs with functions 
 that modify that struct?
My intuition would be, you make vector a struct - since it needs no inheritance, and player a class - since you would want to share the *same* player in different parts of a program, and there are different types of players (inheritance/polymorhism!), and players may get pretty large since they have state, AI, and geometry associated with them.
 and is it ok to imbed a class within a class like
 
 cPlayer
 {
    cVector vector;
 }
 ?
Yes, but don't forget to new or assign the vector before you use it. However, you really hardly want vector to be a class, as seen from your previous thread. -eye
May 25 2004