Looking for brutal criticism, feedback and improvements to my initial OOP design.
My code should be elegant, efficient and easy to understand for beginners.
I am wanting to create an OO system for "items" on say the map, a shop or Player inventory.
I'm not entirely sure about the design of the base class. From what I hear you need to have a common base class between classes so a base class pointer can point to all base classes of derived members. I understand the concept, but do I have to make a base class contain hundreds of pure virtual functions to all different types of derived to avoid slicing? Some light on this issue will be appreciated. It may be because having all game object derived from a single base class may be the wrong way to do things, I just want storing objects in memory to be easy.
If you are going to take string by value, make use of default values and move semantic. And do not forget to mark one-argument constructors as explicit:
1 2
//Instead of both constructors:
explicit Object(std::string n = ""): MyID(ObjID++), Name(std::move(n)) {}
Thanks for the reply MiiNiPaa, I'll read up on the explicit keyword. Is there anything else you can identify as a design flaw? I'm looking for most input on the class design and the derivation of new classes. Of course weapons and shields with have more attributes than what is shown.
Also:
Use default/deleted functions. For example in your case: virtual ~Object() = default;
I would suggest to avoid unnesessary inheritance if possible. If your base class cannot provide usable and safe common interface, it is useless. What is the difference between weapon and armor? What is common? How can you represent them as one entity?
For example you can say that weapons and armor are objects which is equipped to different slots and provide different bonuses.
So your Object can be represented as:
1 2 3 4 5 6 7 8
class Item
{
//...
enum type {WEAPON, ARMOR};
//...
type slot;
std::vector<std::shared_ptr<Effect>> effects;
};
You use slot member to determine what it is and where you can equip it; use effects member to determine effects of eqipping
Another set of excellent suggestions, I really appreciate the help.
std::vector<std::shared_ptr<Effect>> effects;
Hmm... It appears I need to create objects before the Item object. I will take time to better define my classes. I just fear I am going "too far down" the scale of OOP. I don't know to what scale I have to code at, if that makes sense.
Also,
Use default/deleted functions. For example in your case: virtual ~Object() = default;
Exactly the same you did: destructor with empty body. But in this case it would count as default destructible for purpose of different type_traits. It might be not really important here, but it is a good idea to explicitely declare default versions of constructors/destructor/operators as such.