Why so many structures instead classes?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Cppuser
Posts: 12
Joined: Thu Sep 28, 2006 8:38 pm

Why so many structures instead classes?

Post by Cppuser »

Hi,

is there a reason why using a structure for Vertex?
Why not using a class for Vertex?

Is there a technical reason or is it just design style of the coder?
Same for Materialstructure.

thx
gfxstyler
Posts: 222
Joined: Tue Apr 18, 2006 11:47 pm

Post by gfxstyler »

i guess it's just style preference.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Eh, I expect it's probably just that if you want a class with all of its methods and members public, then it saves you typing 6 characters.

I didn't say that was a good reason. You know, unless those are the last 6 bytes of storage on your hard drive, and Alyson Hannigan has just given you her phone number and you need to write it down somewhere. Then you'd be glad you used a struct.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

A struct and a class are basically the same in C++, the only difference being the default protection level [public for struct, private for class]. Since all members of those types are public it kinda makes sense to use a struct.
CodeDog
Posts: 106
Joined: Sat Oct 07, 2006 8:00 pm
Location: CA. USA
Contact:

Post by CodeDog »

Irrlicht uses an older style of programming reminiscent of C in which you encapsulate data in a struct and then make functions that act on structs instead of a class where you encapsulate both the data and the methods and the class acts on itself.
xterminhate
Posts: 206
Joined: Thu Sep 01, 2005 9:26 pm
Location: France

Post by xterminhate »

C++ compilers know how to optimize structs or classes, which have no member functions or methods, into POD (as C struct). Am I right ?
Return to Irrlicht after years... I'm lovin it.
It's hard to be a Man !
Si vis pacem para belum
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

There isn't anything to optimize. If a type is POD it is POD. The compiler does not need to optimize anything because the data is just a block of memory [C-style].
xterminhate
Posts: 206
Joined: Thu Sep 01, 2005 9:26 pm
Location: France

Post by xterminhate »

Sorry, "optimize" was not the correct term. Compilers just detect pod classes, and do not add any default constr, copy/assignment op or destruct. Alright.
Return to Irrlicht after years... I'm lovin it.
It's hard to be a Man !
Si vis pacem para belum
drarem
Posts: 81
Joined: Mon Mar 06, 2006 4:40 am
Contact:

Post by drarem »

I'd be happy if it was just structures and function pointers >:||
Cppuser
Posts: 12
Joined: Thu Sep 28, 2006 8:38 pm

Post by Cppuser »

rogerborg wrote:
I didn't say that was a good reason. You know, unless those are the last 6 bytes of storage on your hard drive, and Alyson Hannigan has just given you her phone number and you need to write it down somewhere. Then you'd be glad you used a struct.
Very funny.

You don't have to held account for something if someone just asks for a technical reason of an implementation.
Just say nothing if you don't know the answer or the reason.
But please don't try again to simulate a good thought behind it.

Because you speak about bytes and performance but not even a vertexbuffer has been used at this time in the engine.
So who is glad about what?

@all
Please don't misunderstand the vertexbuffer comment.
But if someone is talking about saving bytes or having better performance on other parts in the code.
But for many other things, that are much more important the performance was not recognized, then such explanations and codingtheories about saving bytes or having better performance doesn't sound credible for me ...
Because if we talk about saving bytes or performance we should be consistent ... Everything else is just Mickey Mouse.

And thx gfxstyler and vitek your answers confirmed my thoughts that this was just a design style reason.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

You should always try to use the optimal code. That does not mean that you should always save the last dozen of bytes, but choose the proper structures for the requirements of that special part of your code. And if you just want some simple structuring element and also communicate this property to the code user you should use the struct. The fact that you might save some bytes with this decision makes it reasonable (otherwise you might not really want it), but it should not be the only reason. Architecture and clean structures is more important.
Post Reply