Answer the question
In order to leave comments, you need to log in
Is it possible to get the value of a class constructor variable?
I need to get the value (not change) that is assigned to a class object in main();
Here is a class constructor listing:
Warrior(int hl, int at, int lv): health(hl), attack(at), lvl(lv) //constructor hero skills
{}
Warrior knight(rand()%11 + 9, rand()%8 + 6, 1);
health
and use it in another function.
Answer the question
In order to leave comments, you need to log in
class Warrior
{
public:
Warrior(int hl, int at, int lv)
: health_(hl)
, attack_(at)
, lvl_(lv)
{}
public:
int health()
{
return health_;
}
private:
int health_;
int attack_;
int lvl_;
};
int main()
{
Warior knight(0, 0, 0);
your_function(knight.health());
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question