|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
[C++] Class code consolidation?
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
Intel Overclocker
|
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;
}
}
__________________
Last edited by Microsis : 06-20-08 at 05:57 PM. |
|||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
Kernel Sanders
|
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
|
|||||||||||||
|
|
|
|
#3 (permalink) | |||||||||||||
|
Programmer
|
Quote:
Code:
class foo {
public:
void pie(int blah)
{
cout<<"I like pie";
}
}
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.
|
|||||||||||||
|
|
|
|
|
#4 (permalink) | ||||||||||||||
|
Intel Overclocker
|
Quote:
Code:
Class character
{
int m_HP, m_ATK;
//...
};
__________________
|
||||||||||||||
|
|
|
|
|
#5 (permalink) | |||||||||||||
|
Kernel Sanders
|
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.
|
|||||||||||||
|
|
|
|
#6 (permalink) | ||||||
|
New to Overclock.net
|
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
|
||||||
|
|
|
|
|
#7 (permalink) | ||||||||||||||
|
Intel Overclocker
|
Quote:
__________________
|
||||||||||||||
|
|
|
|
|
#8 (permalink) | |||||||||||||
|
Programmer
|
Quote:
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.
|
|||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|