Thread: extern or define in header?

  1. #1
    Join Date
    Aug 2002
    Tucson, Arizona

    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.
    #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
    Now should I do this
    #ifndef weaponAttributes_h
    #define weaponAttributes_h
    #include "weapon.h"
    //name,power,retailPrice,selling price,is it upgradable
    weapon sword = {"sword",10,50,30,false};
    or define the sword into the cpp file and extern it in the header

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Can be done in many ways. I'd make some kind of factory class:
    struct Weapon
      Weapon(const std::string& NewName, int NewPower)
        Name = NewName;
        Power = NewPower;
      std::string Name;
      int Power;
    class Weapons
        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];
        std::map<std::string, Weapon> WeaponList;
    Weapons weapons;
    and to create a new weapon:
    weapons.CreateWeapon("Dagger", 12);
    weapons.CreateWeapon("Sword", 46);
    and to use the weapon:
    Weapon* weapon = weapons.GetWeapon(Player.CurrentWeapon());
    or something...
    Last edited by Magos; 03-07-2005 at 07:00 PM.

    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
    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
    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
Website Security Test