Page 1 of 1

Why so many structures instead classes?

Posted: Tue Nov 14, 2006 7:36 pm
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

Posted: Tue Nov 14, 2006 7:57 pm
by gfxstyler
i guess it's just style preference.

Posted: Tue Nov 14, 2006 8:32 pm
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.

Posted: Tue Nov 14, 2006 9:45 pm
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.

Posted: Wed Nov 15, 2006 6:21 am
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.

Posted: Wed Nov 15, 2006 7:56 am
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 ?

Posted: Wed Nov 15, 2006 10:04 am
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].

Posted: Wed Nov 15, 2006 11:41 am
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.

Posted: Wed Nov 15, 2006 12:47 pm
by drarem
I'd be happy if it was just structures and function pointers >:||

Posted: Thu Nov 16, 2006 11:16 pm
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.

Posted: Fri Nov 17, 2006 12:18 am
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.