extern or define in header?

This is a discussion on extern or define in header? within the C++ Programming forums, part of the General Programming Boards category; Hello guys for another game I am working on I am trying to make a list of types of weapons ...

  1. #1
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812

    extern or define in header?

    Hello guys for another game I am working on I am trying to make a list of types of weapons that you can use and I was wondering how would you would go about this basically I am making pre-defined structs of weapons and armor. Here is the question should I just define them in a header file or should or extern them in the header and define them in the cpp file or is it a matter of taste?
    heres some code if you are confused cause well I do that sometimes.
    Code:
    #ifndef weapon_h
    #define weapon_h
    
    #include <string>
    
    struct weapon
    {
        std::string name;    //Weapon's name;
        int power;           //How much it add's to your attack
        int retailPrice;     //How much that it cost in a shop
        int sellingPrice;    //How much you can sell it for
        bool isUpgradeable;  //If is is allowed to be upgraded
    };
    
    #endif//weapon_h
    Now should I do this
    Code:
    #ifndef weaponAttributes_h
    #define weaponAttributes_h
    
    #include "weapon.h"
    //name,power,retailPrice,selling price,is it upgradable
    
    weapon sword = {"sword",10,50,30,false};
    
    #endif//weaponsAttributes_h
    or define the sword into the cpp file and extern it in the header
    Woop?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Can be done in many ways. I'd make some kind of factory class:
    Code:
    struct Weapon
    {
      Weapon(const std::string& NewName, int NewPower)
      {
        Name = NewName;
        Power = NewPower;
      }
    
      std::string Name;
      int Power;
    };
    
    class Weapons
    {
      public:
        void CreateWeapon(const std::string& Name, int Power)
        {
          WeaponList.insert(std::make_pair(Name, Weapon(Name, Power)));
        }
    
        Weapon* GetWeapon(const std::string& Name)
        {
           return &WeaponList[Name];
        }
    
      protected:
        std::map<std::string, Weapon> WeaponList;
    };
    
    Weapons weapons;
    and to create a new weapon:
    Code:
    weapons.CreateWeapon("Dagger", 12);
    weapons.CreateWeapon("Sword", 46);
    and to use the weapon:
    Code:
    Weapon* weapon = weapons.GetWeapon(Player.CurrentWeapon());
    enemy.InflictDamage(weapon->Power);
    or something...
    Last edited by Magos; 03-07-2005 at 07:00 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21