I've got sth like that:
#include <iostream>
using namespace std;
class FirstClass
{
public:
FirstClass(int _vx) : vx(_vx) {}
int x () {return vx;}
private:
int vx;
};
It's pretty obvious, that I have to write:
int main ()
{
FirstClass Object1(666);
cout << Object1.x();
return 0;
}
And everything is ok. But problem is, when I want to have this in another class:
#include <iostream>
using namespace std;
class FirstClass
{
public:
FirstClass(int _vx) : vx(_vx) {}
int x () {return vx;}
private:
int vx;
};
class SecondClass
{
public:
FirstClass Member1(666);
};
int main ()
{
SecondClass Object2;
cout << Object2.Member1.x();
return 0;
}
I can't even compile it. So, my question is: how can I pass arguments to constructor of FirstClass in a declaration of SecondClass?
Thanks in advance.
You've got to pass the initialization value to FirstClass in the constructor of SecondClass, like this:
class SecondClass:
{
public:
SecondClass() : Member1(666) {}
Firstclass Member1;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With