# TypeDef or Inheritance

## Recommended Posts

Hiya I am trying to make an RPG and to start it I am trying to make the classes for all the items and characters. I am only starting C++ though I have bought a few books about it, looking through them two have different approaches on Weapons class. Could you please tell me the Pros and Cons of the two approaches. 1. typedef class cWeapon { public: String _name; int _weigth; int _attack; int _baseprice; void OnAttack(cWeapon* Weapon); } cWeapon; Making Weapons like this: cWeapon Shortsword = {"Short Sword", 10, 5, 30}; The OnAttack function takes in the Weapon and places it in a switch thingy that then does different things depending on the weapon. 2. class Weapon { public: int _weight; int _attack; int _basePrice; virtual void OnAttack(int *_pdamage) = 0; }; Making Weapons like this: class Longsword : public Weapon { public: void OnAttack(int *_pdamage); }; The OnAttack function is made differently for each Weapon so it doesn't need to take in the Weapon itself.

##### Share on other sites
Option 2 is what i would go for but bearing in mind that the only real advantage is if you intend to have a polymorphic collection of all weapons and also want to make the ability to create new weapons easier for say the end user.

ace

##### Share on other sites
Quote:
 Original post by Peter ConnThe OnAttack function takes in the Weapon and places it in a switch thingy that then does different things depending on the weapon.

Avoid type switching (link to pdf, check page 17 onwards).

Enigma

##### Share on other sites
0) As noted, go with the polymorphic approach when "different things" need to be done. That's what the language feature is there for. There is effectively no manual way to do it more efficiently; if you could think of one, I'm sure there's a compiler-writing company somewhere that would like to hire you.

However, for weapons, there is a good chance that all you need to do is something like { target->takeDamage(_attack); } - in which case no polymorphism or switching of any kind is required. You can always change it later.

1) The "typedef struct idiom" is a C-ism. It has no place in C++ at all (except perhaps where required for backwards compatibility with someone else's C code).

2) For strings, use std::string. It is declared in header <string>, is in the namespace std (you should not put 'using namespace' declarations in your header where you declare the class, because it will affect other files perhaps adversely, but they are fine in implementation files generally), and is spelled with a lowercase s.

3) As written, OnAttack() is a member function in the first case - thus there is already a weapon available; passing in another one makes no sense. Also, this name makes no sense in an OO context (it is good naming when you're working with someone else's event-driven framework though); the name would normally just be attack().

4) However, normally an attack requires that you specify what it is you're attacking :) That would be a parameter (e.g. of some Player type), which would provide some function like takeDamage(), that you would call from here.

5) All that data should be private, and exposed only as needed. The class should do the work that makes sense for it - that is, you don't write "getAttackValue()" and then use it to call takeDamage(int) on a Player, but instead write "attack(const Player&)" which then calls takeDamage(int) on the provided Player directly.

##### Share on other sites
1. The typedef is totally unnecessary. Using typedef like that is usually done in C code to make it look more like C++.
2. You don't need the cWeapon * parameter for the OnAttack function. OnAttack should be declared like this:
    void OnAttack();
and called like this:
    cWeapon * weapon;    ...    weapon->OnAttack();
Edit: Oh, I see Zahlman already covered these.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628381
• Total Posts
2982360

• 10
• 9
• 15
• 24
• 11