Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [C++] Class code consolidation?
New Posts  All Forums:Forum Nav:

[C++] Class code consolidation?

post #1 of 8
Thread Starter 
I'm about to throw the following class into it's own header file...

I was just wondering if there are any changes i should make (consolidating etc.) you c++ gurus would recommend.

Also, just making sure my uses of inline and const are correct.

Code:
class Mob
{
public:
     Mob(int hp = 50, string name = "Giant Roach", int attack = 9); //if nothing is set, default to 50

     int getHP() const;
     string getName() const;
     int getAttack() const;
     bool dmg(int& dmg);
     int attack(int& playerHP);
private:
     int m_HP;
     string m_Name;
     int m_Attack;
};

Mob::Mob(int hp, string name, int attack):
             m_HP(hp),
             m_Name(name),
             m_Attack(attack)
{
     cout << "t--" << getName() << " created with " << getHP() << " HP" << endl << endl;
}
inline int Mob::getHP() const
{
     return m_HP;
}
inline string Mob::getName() const
{
     return m_Name;
}
inline int Mob::getAttack() const
{
     return m_Attack;
}
inline int Mob::attack(int& playerHP)
{
     playerHP -= getAttack();
     cout << "t" << getName() << " attacks you for " << getAttack() << " damage" << endl;
     cout << "tYou now have " << playerHP << " HP" << endl << endl;
     return playerHP;
}
inline bool Mob::dmg(int& dmg)
{
     m_HP -= dmg;  
     cout << "tYou deal " << dmg << " damage!" << endl;
     if (m_HP <= 0) //killing blow
     {
          m_HP = 0; cout << "tYou killed " << getName() << "!" << endl << endl;
          return 0;
     }
     else //non-killing blow
     {
          cout << "t" << getName() << " now has " << getHP() << "HP" << endl << endl;
          return 1;
     }
}

Edited by Microsis - 6/20/08 at 2:57pm
Blitz II
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 MSI P55-GD65 GTX 660Ti 4GB Corsair Dominator 1600MHz 
Hard DriveOSMonitorKeyboard
600GB (2x300GB Velociraptors RAID0) Windows 10 24" ASUS VW246H Logitech Illuminated 
PowerCaseMouseMouse Pad
Corsair 750HX Lian Li Lancool PC-K62 G5/DeathAdder Desk 
Audio
Yamaha H80M + HS10W 
  hide details  
Reply
Blitz II
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 MSI P55-GD65 GTX 660Ti 4GB Corsair Dominator 1600MHz 
Hard DriveOSMonitorKeyboard
600GB (2x300GB Velociraptors RAID0) Windows 10 24" ASUS VW246H Logitech Illuminated 
PowerCaseMouseMouse Pad
Corsair 750HX Lian Li Lancool PC-K62 G5/DeathAdder Desk 
Audio
Yamaha H80M + HS10W 
  hide details  
Reply
post #2 of 8
You only really want to use inline for very short methods. What it means is that when the compiler comes upon a call to that function, it replaces the function call with just the code that the method contains. So in the compiled program, everywhere that getHP is called, there is no actual function call, just the equivalent of "return m_HP." There is overhead associated with a function call, so for very short functions like that one it improves performance to make it inline. If you use inline for a function such as Mob::dmg, it will also replace all calls to Mob::dmg with the code for it. Since this is a longer function, that will make your compiled program much bigger.

You can use inline for all of them, but I would suggest only using inline for one or two line functions
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #3 of 8
Quote:
Originally Posted by rabidgnome229 View Post
You only really want to use inline for very short methods. What it means is that when the compiler comes upon a call to that function, it replaces the function call with just the code that the method contains. So in the compiled program, everywhere that getHP is called, there is no actual function call, just the equivalent of "return m_HP." There is overhead associated with a function call, so for very short functions like that one it improves performance to make it inline. If you use inline for a function such as Mob::dmg, it will also replace all calls to Mob::dmg with the code for it. Since this is a longer function, that will make your compiled program much bigger.

You can use inline for all of them, but I would suggest only using inline for one or two line functions
To be honest. The compiler is much better at choosing inline functions than a programmer. I would not set any as inline, let the compiler do it, that's its job. Also, you aren't putting the functions into that header file as well are you? If you are, i would recommend doing so like this.


Code:
class foo {

    public:
 
        void pie(int blah)
        {
               cout<<"I like pie";
        }


}
^^ Should be correct with GCC. But its late so meh.

Another thing to think about, is creating a standard entity class that most other classes are derived from. Its a fair amount of work, but it pays off in the end big time.
UberRig
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2D E8200 @ 3.75ghz (1.375v, 468 *4 = 1875mhz FSB) ASUS P5N-E SLI (650i) EVGA 9600 GT (KO Edition) OCZ 2GB Dual Channel Platinum Revision 2 X (2x1GB) 
Hard DriveOptical DriveOSMonitor
WD Caviar 250gb + 80gb Sony DVDRW Vista x64/Mint x64 Philips Brilliance (20" LCD) 
PowerCase
Huntkey Green Star 500w Thermaltake Soprano 
  hide details  
Reply
UberRig
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2D E8200 @ 3.75ghz (1.375v, 468 *4 = 1875mhz FSB) ASUS P5N-E SLI (650i) EVGA 9600 GT (KO Edition) OCZ 2GB Dual Channel Platinum Revision 2 X (2x1GB) 
Hard DriveOptical DriveOSMonitor
WD Caviar 250gb + 80gb Sony DVDRW Vista x64/Mint x64 Philips Brilliance (20" LCD) 
PowerCase
Huntkey Green Star 500w Thermaltake Soprano 
  hide details  
Reply
post #4 of 8
Thread Starter 
Quote:
Originally Posted by Butterbum View Post
Another thing to think about, is creating a standard entity class that most other classes are derived from. Its a fair amount of work, but it pays off in the end big time.
such as..

Code:
Class character
{
   int m_HP, m_ATK;
   //...
};
and then Class Mob and Class Player are childs of that (parent) class?
Blitz II
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 MSI P55-GD65 GTX 660Ti 4GB Corsair Dominator 1600MHz 
Hard DriveOSMonitorKeyboard
600GB (2x300GB Velociraptors RAID0) Windows 10 24" ASUS VW246H Logitech Illuminated 
PowerCaseMouseMouse Pad
Corsair 750HX Lian Li Lancool PC-K62 G5/DeathAdder Desk 
Audio
Yamaha H80M + HS10W 
  hide details  
Reply
Blitz II
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 MSI P55-GD65 GTX 660Ti 4GB Corsair Dominator 1600MHz 
Hard DriveOSMonitorKeyboard
600GB (2x300GB Velociraptors RAID0) Windows 10 24" ASUS VW246H Logitech Illuminated 
PowerCaseMouseMouse Pad
Corsair 750HX Lian Li Lancool PC-K62 G5/DeathAdder Desk 
Audio
Yamaha H80M + HS10W 
  hide details  
Reply
post #5 of 8
Quote:
Originally Posted by Microsis View Post
such as..

Code:
Class character
{
   int m_HP, m_ATK;
   //...
};
and then Class Mob and Class Player are childs of that (parent) class?
That is good OO practice in general, but I believe that Butter was telling you to make a class analogous to java's Object class, so that even the generic character class would derive from it.
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #6 of 8
Its been around 2-3 years since i was doing C++ stuff.... been doing web stuff latley...

I was wondering why you were unsing inline anyways. I dont remember needing to use inline with classes. i designed something like that. it was a 1v1 game where you allocate attributes and you select abilitys. I never used inline throughout the whole thing. Was a few hundred lines of code heh... kinda sad that i lost it.

Inline has its advantages and disadvantages. Just doesn't seem needed In my opinion. But then again im probably average or a little below average programmer.

Here is some more info on it.
http://www.parashift.com/c++-faq-lit...functions.html
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
post #7 of 8
Thread Starter 
Quote:
Originally Posted by Mrzev View Post
Its been around 2-3 years since i was doing C++ stuff.... been doing web stuff latley...

I was wondering why you were unsing inline anyways. I dont remember needing to use inline with classes. i designed something like that. it was a 1v1 game where you allocate attributes and you select abilitys. I never used inline throughout the whole thing. Was a few hundred lines of code heh... kinda sad that i lost it.

Inline has its advantages and disadvantages. Just doesn't seem needed In my opinion. But then again im probably average or a little below average programmer.

Here is some more info on it.
http://www.parashift.com/c++-faq-lit...functions.html
yea i've been reading forums and most people recommend just letting the compiler decide when it will use a function as an inline or not.
Blitz II
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 MSI P55-GD65 GTX 660Ti 4GB Corsair Dominator 1600MHz 
Hard DriveOSMonitorKeyboard
600GB (2x300GB Velociraptors RAID0) Windows 10 24" ASUS VW246H Logitech Illuminated 
PowerCaseMouseMouse Pad
Corsair 750HX Lian Li Lancool PC-K62 G5/DeathAdder Desk 
Audio
Yamaha H80M + HS10W 
  hide details  
Reply
Blitz II
(14 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 MSI P55-GD65 GTX 660Ti 4GB Corsair Dominator 1600MHz 
Hard DriveOSMonitorKeyboard
600GB (2x300GB Velociraptors RAID0) Windows 10 24" ASUS VW246H Logitech Illuminated 
PowerCaseMouseMouse Pad
Corsair 750HX Lian Li Lancool PC-K62 G5/DeathAdder Desk 
Audio
Yamaha H80M + HS10W 
  hide details  
Reply
post #8 of 8
Quote:
Originally Posted by Microsis View Post
such as..

Code:
Class character
{
   int m_HP, m_ATK;
   //...
};
and then Class Mob and Class Player are childs of that (parent) class?
Yea, i did mean an "object" class similar to Java's. But you could do this too, i would probably use the name entity. You would store stuff like position, image information etc in this class.

Like i said before, its best to leave the inline decision up to the compiler, unless you're writing the next AAA game, and performance is a serious issue. For all you know, the functions you're choosing as inline could effect the performance of the program negatively.
UberRig
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2D E8200 @ 3.75ghz (1.375v, 468 *4 = 1875mhz FSB) ASUS P5N-E SLI (650i) EVGA 9600 GT (KO Edition) OCZ 2GB Dual Channel Platinum Revision 2 X (2x1GB) 
Hard DriveOptical DriveOSMonitor
WD Caviar 250gb + 80gb Sony DVDRW Vista x64/Mint x64 Philips Brilliance (20" LCD) 
PowerCase
Huntkey Green Star 500w Thermaltake Soprano 
  hide details  
Reply
UberRig
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2D E8200 @ 3.75ghz (1.375v, 468 *4 = 1875mhz FSB) ASUS P5N-E SLI (650i) EVGA 9600 GT (KO Edition) OCZ 2GB Dual Channel Platinum Revision 2 X (2x1GB) 
Hard DriveOptical DriveOSMonitor
WD Caviar 250gb + 80gb Sony DVDRW Vista x64/Mint x64 Philips Brilliance (20" LCD) 
PowerCase
Huntkey Green Star 500w Thermaltake Soprano 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [C++] Class code consolidation?