Typedef vs. Class
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?
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.
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?
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.
Classes are better than typedefs, but functions are better than both.
Variables and macros are somewhere in between.
Variables and macros are somewhere in between.
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.
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.
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.
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]
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
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
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:
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]
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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement