I want to write some easy-to-call object initializiation and I would like those objects to be shared_ptr of MyClass.
Just for visualisiation purposes, imagine this class:
class MyClass
{
public:
MyClass(int number);
MyClass(wxString path);
}
int main()
{
//For now, objects are created in c style like this:
MyClass obj1("C:\\test");
MyClass * obj2 = new MyClass(5);
}
Now I would like to change this with easy to use smart pointer initialization. My goal is something looking like a constructor call, so that very little has to be changed to update the old call to the new one:
Smart_MyClass obj1("C:\\test");
Smart_MyClass obj2(5);
//where Smart_MyClass is std::shared_ptr<MyClass>
I could solve this with a function:
template<typename T>
inline Smart_MyClass Smart_MyClass_Fct(T t)
{
return std::make_shared<MyClass>(t);
};
Smart_MyClass obj = Smart_MyClass_Fct(7);
How can I translate that into the form I need? I could think of using the operator(), that would require a struct/class and maybe inheritence of my actual class? And maybe casting it back to parent class? Pretty sure there is something more elegant. ;-)