Class's VS structs...in RPG engine
I am building an RPG engine and to control charactor structures, such as
Charactor.Health
should i use classes, or should i use structures? In means of keeping mem usage low and not eating resources along.
Instead of having
typedef struct {
int health,
dexterity,
...and so on
}
pCharactor;
and do calculations to the variables from different functions?
or should i use a class
class Charactor {
public:
Charactor() {}
~Charactor() {}
int do_calculations() { ..some code }
private:
short health,
dextarity
...and so on
}
and have the functions to manipulate data withint he class and keep data hidden? Give me some input on what would be best for an RPG engine.
Thnx
Assuming that you are going to use the same member variables in either a struct or a class, there is virtually no difference as far as memory usage goes. In fact, you will save memory with classes if you normally end up repeating the same operations in your code in different places. C++ member functions have no more memory footprint than a normal function and there is only one ''copy'' of each member function in memory that all objects of that class use.
I would recommend using C++ and hiding the data as you suggest. This provides many benefits that help for debugging and code maintenance. It also makes it very easy to control whether, for example, a character''s health is in a valid range (or being set to a valid range)
I would recommend using C++ and hiding the data as you suggest. This provides many benefits that help for debugging and code maintenance. It also makes it very easy to control whether, for example, a character''s health is in a valid range (or being set to a valid range)
Yes, I thoroughly recommend classes. Not only will it be cleaner, it will be super-easy to handle. IMHO, it is much easier to manage a dynamic array of characters (or something of the like) if they are classes than if they are structs. Classes have contsructors and destructors, which are of EXTREME use as far as memory management goes
.
Classes over structs almost always, imho.:D

Classes over structs almost always, imho.:D
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement