Typecasting

This is a discussion on Typecasting within the C++ Programming forums, part of the General Programming Boards category; I have read over some on the subject. If I have a base class that is publicly inherited to 7 ...

  1. #1
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    319

    Typecasting

    I have read over some on the subject. If I have a base class that is publicly inherited to 7 more classes each with specific deals on them, but I want to have a storage container of just the base class since every child class has a static database to get info for said object from.

    From what I read I can just use static_cast and be happy and all, but would it be better to use a dynamic_cast for casting from derived to base runtime wise?

  2. #2
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    A dynamic_cast is safer than static_cast because it checks at runtime wheter the cast is allowed.

    See dynamic_cast [C++ Reference] and static_cast [C++ Reference]

  3. #3
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    319
    While true there is no way to cast from base to derived except static casting correct?

  4. #4
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    319
    My implementation would be an inventory in a game. We store items in there go figure. So base class item, but items can be weapons, armor, crafting materials, quest items, ammunitions, crafting designs, or useables. Now approaching this issue I thought a base class of Item would work with derived classes for each of the more specific items would work. Now when you have something in your inventory it is of the base class, but lets say we want to equip it ok so I would need to use a static_cast<Armor> Item_ptr now for the internal working of the classes each will load a database with information specific to that kind of item so weapons will have damage specs and armor will have defensive specs. Is this a good way to approach this problem? I would think it would be more memory efficent than giving every item stats it may not ever use.

    Code:
    #ifndef ITEM_H
    #define ITEM_H
    
    class Item
    {
        public:
        Item(int ID, int Mod, int Mods[8])
        {
            id = ID;
            Modifier = Mod;
            for(int i=0;i<8;i++)Modifacations[i] = Mods[i];
        }
        void Set_Item(int ID,int Mod, int Mods[8])
        {
            id = ID;
            Modifier = Mod;
            for(int i=0;i<8;i++)Modifacations[i] = Mods[i];
        }
        Item & operator|(Item &rhs)   //swap function
        {
            int temp;
            temp = rhs.id;
            rhs.id = id;
            id = temp;
            temp = rhs.Modifier;
            rhs.Modifier = Modifier;
            Modifier = temp;
            for(int i=0;i<8;i++)
            {
                temp = Modifacations[i];
                Modifacations[i] = rhs.Modifacations[i];
                rhs.Modifacations[i] = temp;
            }
            return *this;
        }
    
        void Set_Modifiers(int new_mods[8]){for(int i=0;i<8;i++)Modifacations[i] = new_mods[i];}
    
        int Get_ID(){return id;}
        int Get_Modifier(){return Modifier;}
    
        private:
        int id;                                 //
        int Modifier;                           //Crafted modifier
        int Modifacations[8];
    };
    #endif
    Code:
    #ifndef WEAPON_H
    #define WEAPON_H
    
    #include "Datatypes.h"  //contains Damage struct for storing damage
    #include "Item.h"       //Base class the very most basic definition of an item
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    class Weapon:public Item
    {
      public:
    
      Weapon(int ID, int Mod,int new_mods[8]) : Item(ID,Mod,new_mods)
      {
         
      }
      Weapon & operator=(const Weapon &other)
      {
         Item::operator=(other);
         min_dmg = other.min_dmg;
         max_dmg = other.max_dmg;
         hands = other.hands;
         speed = other.speed;
         range = other.range;
    
         return *this;
      }
      Weapon & operator|(Weapon &other)
      {
         Item::operator|(other);
         int temp;
    
         temp = min_dmg;
         min_dmg = other.min_dmg;
         other.min_dmg = temp;
    
         temp = max_dmg;
         max_dmg = other.max_dmg;
         other.max_dmg = temp;
    
         temp = hands;
         hands = other.hands;
         other.hands = temp;
    
         temp = speed;
         speed = other.speed;
         other.speed = temp;
    
         temp = range;
         range = other.range;
         other.range = temp;
    
         return *this;
      }
    
      Damage Get_Damage()
      {
          Damage *dmgptr;
          dmgptr = new Damage;
          dmgptr->norm = (int)((Get_Modifier()/100)*(min_dmg+rand()%(max_dmg-min_dmg)));
          dmgptr->fire = 0;
          dmgptr->ice = 0;
          dmgptr->poison = 0;
          return *dmgptr;
      }
    
      int Get_Hands()   {return hands;}
      int Get_Speed()   {return speed;}
      int Get_Range()   {return range;}
      int Get_Slot()    {return 0;}
    
      static void LOAD_DATA(char *data_file_path)
      {
          LOADED = true;
          ifstream stream;
          stream.open(data_file_path);
          stream>>nmbr;
          infolist = new weapon_info*[nmbr];
          for(int i=0;i<nmbr;i++)
          {
             infolist[i] = new weapon_info;
             stream>>infolist[i]->id
                   >>infolist[i]->mdmg
                   >>infolist[i]->xdmg
                   >>infolist[i]->range
                   >>infolist[i]->speed
                   >>infolist[i]->hands;
          }
      }
    
      private:
      static int nmbr;
      static bool LOADED;
      static weapon_info **infolist;
      int min_dmg, max_dmg;
      int hands;
      int speed;
      int range;
    
    };
    
    #endif
    Last edited by ~Kyo~; 05-10-2011 at 06:02 PM. Reason: Adding code to explain text.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Typecasting Help
    By 127.0.0.1 in forum C Programming
    Replies: 7
    Last Post: 03-11-2010, 01:26 AM
  2. Help with typecasting
    By netto in forum C++ Programming
    Replies: 8
    Last Post: 09-14-2009, 06:08 PM
  3. typecasting
    By eklavya8 in forum C Programming
    Replies: 9
    Last Post: 01-03-2009, 08:02 AM
  4. typecasting
    By JaWiB in forum C++ Programming
    Replies: 14
    Last Post: 06-01-2003, 10:42 PM
  5. typecasting?
    By tammo21 in forum C Programming
    Replies: 6
    Last Post: 08-17-2002, 04:19 AM

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