I
I
Ivan Ivanov2014-04-11 18:29:36
C++ / C#
Ivan Ivanov, 2014-04-11 18:29:36

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

Listing of the object in which health, attack, lvl is initialized :
Warrior knight(rand()%11 + 9, rand()%8 + 6, 1);
It turns out that you need to find out the value healthand use it in another function.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tsarevfs, 2014-04-11
@Csklassami

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 question

Ask a Question

731 491 924 answers to any question