Archived

This topic is now archived and is closed to further replies.

Typedef vs. Class

This topic is 5012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m programming in c++ and say I want to make a parameter structure for use with a class that is the manager of the data in a linked list of this parameter structure, anyway, is there any gain or loss when using a class vs a typedef or vice versa for a structure?

Share this post


Link to post
Share on other sites
You might want to show some code to explain what you''re trying to decide between. The way you have your post phrased I can''t figure out which of three different things you''re talking about.

Share this post


Link to post
Share on other sites
ok, im trying to make a particle system, and what i plan to do is make a class that will manage all the particles created and the way it will store the particles is in a linked list, the list is going to be of either a class with only variables, no member functions, or a typedef structure of variables, my questions is whether either one is more efficent or more professional?

Share this post


Link to post
Share on other sites
I wouldn''t use a linked list. You''ll get much better performance from an dynamically created array. Mark dead particles as ''dead'' and reuse them, rather then continually scour the list.

Share this post


Link to post
Share on other sites
There will be no difference. Neither is more professional. If you later decide your typedef needs more than 3 or 4 member functions, then switch it to a class. There should be no change in behavior at all.

quote:
Original post by GroZZleR
I wouldn''t use a linked list. You''ll get much better performance from an dynamically created array. Mark dead particles as ''dead'' and reuse them, rather then continually scour the list.

Why is that? With an array, you have to continually scour the list for living particles. Performance with a linked list really depends on the life of the particles, and how often they enter and exit the world. An array on the other hand would always have to skip over dead particles.

Share this post


Link to post
Share on other sites
quote:
Original post by Jiia
Why is that? With an array, you have to continually scour the list for living particles. Performance with a linked list really depends on the life of the particles, and how often they enter and exit the world. An array on the other hand would always have to skip over dead particles.

There would be far fewer cache misses (likely none) with an array, and there would be no allocations/deallocations made when particles die or are spawned. With a list on the other hand, you'll need the expensive allocations (if not you still have to skip over dead particles plus you get cache misses).

In any case, the array can be sorted after a fashion. The last active particle in the list can be copied into the empty slot when a particle further up the list dies. Then there will be no skipping while iterating through the particles.

[edited by - glassJAw on March 25, 2004 10:48:33 PM]

Share this post


Link to post
Share on other sites
i''m using classes, but i think it matters very little. Since each of my particles are unique (color, direction, speed, gravity, fadeout, trail, etc), they are created and destroyed using STL containers instead of being recycled. (Seems cleaner and the costs involved are not a big issue. Updating is *much* more of an issue)

You are welcomed to take apart my simple system if you''d like. i just posted it somewhere else this morning. It''s in SDL:

Particle Demo

Share this post


Link to post
Share on other sites
It sounds to me like you're confusing classes in C++, structs as they were used in C, and typedefs. A struct in C often has the structure:

typedef struct {
...stuff...
} Thing;

This actually defines an anonymous structure and gives it the new name "Thing". This is because in C if you did it the usual way (like classes in C++: ie struct Thing { ... }; ) you had to put struct Thing x; to instantiate it. The typedef gets around this and lets you just say Thing x; In C++ you no longer need to specify that it is a struct (or class), you'd just say Thing x;

All a typedef does is give some type a new name. It helps get around that old C problem of having to say each time "Yes, this name is a struct" each time you try to instantiate it. It is not necessary in C++. A typedef is not a struct.

In C++, struct and class are equivalent except that members and variables default to public in a struct and private in a class. (There are some other obscure differences that aren't worth mentioning.) If you really don't care about encapsulation of that data, go with a struct. It'll save you several keystrokes (yippie).

EDIT: I hate it when code creates smileys.

[edited by - Doc on March 25, 2004 11:23:09 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Jiia
There will be no difference. Neither is more professional. If you later decide your typedef needs more than 3 or 4 member functions, then switch it to a class. There should be no change in behavior at all.

quote:
Original post by GroZZleR
I wouldn''t use a linked list. You''ll get much better performance from an dynamically created array. Mark dead particles as ''dead'' and reuse them, rather then continually scour the list.

Why is that? With an array, you have to continually scour the list for living particles. Performance with a linked list really depends on the life of the particles, and how often they enter and exit the world. An array on the other hand would always have to skip over dead particles.


I''ll take a single if statement over theoretically hundreads of new and deletes every frame. =)

Share this post


Link to post
Share on other sites