Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 06-20-08   #1 (permalink)
Intel Overclocker
 
Microsis's Avatar
 
intel nvidia

Join Date: Apr 2008
Posts: 984

Rep: 124 Microsis is acknowledged by manyMicrosis is acknowledged by many
Unique Rep: 0
Trader Rating: 0
Default [C++] Class code consolidation?

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;
     }
}

System: For The Win
CPU
Q6600 @ stock (for summer)
Motherboard
EVGA 750i FTW
Memory
2x2GB Crucial Ballistix @ stock (for summer)
Graphics Card
XFX 8800GT (700/1000)
Hard Drive
300GB VelociRaptor, 36GB Raptor, 250GB Caviar SE16
Sound Card
X-Fi XtremeMusic
Power Supply
580W Hiper Type-R
Case
Cooler Master CM690
CPU cooling
Tuniq Tower
GPU cooling
Thermaltake Duorb
OS
Windows Ultimate x64
Monitor
22" Samsung 226BW

Last edited by Microsis : 06-20-08 at 05:57 PM.
Microsis is offline   Reply With Quote
Old 06-20-08   #2 (permalink)
Apple Doesn't Love You
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,952
Blog Entries: 1

Rep: 560 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 1
FAQs Submitted: 6
Trader Rating: 5
Default

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
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 06-21-08   #3 (permalink)
Programmer
 
Butterbum's Avatar
 
intel nvidia

Join Date: Jun 2008
Location: London
Posts: 336

Rep: 21 Butterbum is acknowledged by some
Unique Rep: 0
Folding Team Rank: 760
Trader Rating: 0
Default

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.
__________________
System: UberRig
CPU
C2D E8200 @ 3.75ghz (1.375v, 468 *4 = 1875mhz FSB)
Motherboard
ASUS P5N-E SLI (650i)
Memory
OCZ 2GB Dual Channel Platinum Revision 2 X (2x1GB)
Graphics Card
EVGA 9600 GT (KO Edition)
Hard Drive
WD Caviar 250gb + 80gb
Sound Card
Soundblaster Live
Power Supply
Huntkey Green Star 500w
Case
Thermaltake Soprano
CPU cooling
Zalman 9500
GPU cooling
Stock
OS
Vista x64/Ubuntu 8.04
Monitor
Philips 200W (20inch LCD)
Butterbum is offline I fold for Overclock.net   Reply With Quote
Old 06-22-08   #4 (permalink)
Intel Overclocker
 
Microsis's Avatar
 
intel nvidia

Join Date: Apr 2008
Posts: 984

Rep: 124 Microsis is acknowledged by manyMicrosis is acknowledged by many
Unique Rep: 0
Trader Rating: 0
Default

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?

System: For The Win
CPU
Q6600 @ stock (for summer)
Motherboard
EVGA 750i FTW
Memory
2x2GB Crucial Ballistix @ stock (for summer)
Graphics Card
XFX 8800GT (700/1000)
Hard Drive
300GB VelociRaptor, 36GB Raptor, 250GB Caviar SE16
Sound Card
X-Fi XtremeMusic
Power Supply
580W Hiper Type-R
Case
Cooler Master CM690
CPU cooling
Tuniq Tower
GPU cooling
Thermaltake Duorb
OS
Windows Ultimate x64
Monitor
22" Samsung 226BW
Microsis is offline   Reply With Quote
Old 06-22-08   #5 (permalink)
Apple Doesn't Love You
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,952
Blog Entries: 1

Rep: 560 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 1
FAQs Submitted: 6
Trader Rating: 5
Default

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.
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 06-22-08   #6 (permalink)
New to Overclock.net
 
amd nvidia

Join Date: Feb 2008
Posts: 183

Rep: 10 Mrzev Unknown
Unique Rep: 0
Trader Rating: 0
Default

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
__________________
System: Zev's Comp
CPU
AMD Athlon 64 X2 4400+ 2.2ghz
Motherboard
DFI-LANPARTY nF4 SLI-DR
Memory
OCZ Platinum 3gb (2x1gb) +(2x512meg)
Graphics Card
EVGA GeForce 8800GTS 640MB 320-bit GDDR3 PCI Expre
Hard Drive
300gig WD 7200RPM SATA
Mrzev is offline   Reply With Quote
Old 06-22-08   #7 (permalink)
Intel Overclocker
 
Microsis's Avatar
 
intel nvidia

Join Date: Apr 2008
Posts: 984

Rep: 124 Microsis is acknowledged by manyMicrosis is acknowledged by many
Unique Rep: 0
Trader Rating: 0
Default

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.

System: For The Win
CPU
Q6600 @ stock (for summer)
Motherboard
EVGA 750i FTW
Memory
2x2GB Crucial Ballistix @ stock (for summer)
Graphics Card
XFX 8800GT (700/1000)
Hard Drive
300GB VelociRaptor, 36GB Raptor, 250GB Caviar SE16
Sound Card
X-Fi XtremeMusic
Power Supply
580W Hiper Type-R
Case
Cooler Master CM690
CPU cooling
Tuniq Tower
GPU cooling
Thermaltake Duorb
OS
Windows Ultimate x64
Monitor
22" Samsung 226BW
Microsis is offline   Reply With Quote
Old 06-24-08   #8 (permalink)
Programmer
 
Butterbum's Avatar
 
intel nvidia

Join Date: Jun 2008
Location: London
Posts: 336

Rep: 21 Butterbum is acknowledged by some
Unique Rep: 0
Folding Team Rank: 760
Trader Rating: 0
Default

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.
__________________
System: UberRig
CPU
C2D E8200 @ 3.75ghz (1.375v, 468 *4 = 1875mhz FSB)
Motherboard
ASUS P5N-E SLI (650i)
Memory
OCZ 2GB Dual Channel Platinum Revision 2 X (2x1GB)
Graphics Card
EVGA 9600 GT (KO Edition)
Hard Drive
WD Caviar 250gb + 80gb
Sound Card
Soundblaster Live
Power Supply
Huntkey Green Star 500w
Case
Thermaltake Soprano
CPU cooling
Zalman 9500
GPU cooling
Stock
OS
Vista x64/Ubuntu 8.04
Monitor
Philips 200W (20inch LCD)
Butterbum is offline I fold for Overclock.net   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 09:38 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.19840 seconds with 9 queries