Sign in to follow this  
Peter Conn

TypeDef or Inheritance

Recommended Posts

Peter Conn    126
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 this post


Link to post
Share on other sites
dave    2187
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 this post


Link to post
Share on other sites
Enigma    1410
Quote:
Original post by Peter Conn
The 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 this post


Link to post
Share on other sites
Zahlman    1682
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 this post


Link to post
Share on other sites
JohnBolton    1372
Two quick comments about the first case:
  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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this