I am coming from the Java background. I have the following program.
#include <string>
#include <iostream>
class First {
public:
First(int someVal): a(someVal) {
}
int a;
};
class Second {
public:
First first;
Second() { // The other option would be to add default value as ": first(0)"
first = First(123);
}
};
int main()
{
Second second;
std::cout << "hello" << second.first.a << std::endl;
}
In class Second
, I wanted to variable first
to remain uninitialized until I specifically initialize it in Second()'s constructor
. Is there a way to do it? Or am I just left with 2 options?:
- Provide a parameter-less constructor.
- Initialize it with some default value and later re-assign the required value.
I can't initialize first
in the initializer-list with the right value, since the value is obtained after some operation. So, the actual required value for first
is available in Second()
constructor only.