I'm trying to add a lazy-initialization function to my class. I'm not very proficient with C++. Can someone please tell me how I achieve it.
My class has a private member defined as:
std::unique_ptr<Animal> animal;
Here's the original constructor that takes one parameter:
MyClass::MyClass(string file) :
animal(new Animal(file))
{}
I just added a parameter-less constructor and an Init() function. Here's the Init function I just added:
void MyClass::Init(string file)
{
this->animal = ???;
}
What do I need to write there to make it equivalent to what constructor is doing?
I think animal.reset(new Animal(file)) is what you want.