I have a template class I am creating and I need a method that returns an object of that class and assigns it. I have a method that takes in 2 objects, creates a new one, and returns it, as well as an overload of the assignment operator to copy members correctly.
I have tried to do this 2 ways (both methods are static).
Method 1: Works, but isn't this bad practice?
template <class T>
MyClass<T>* MyClass<T>::MyFunction(const MyClass& a, const MyClass& b)
{
MyClass<T> *newObject= new MyClass<T>(a.member, b.member2);
//...do stuff to the object
return newObject;
}
Method 2: Doesn't work, but isn't this a better line of thought?
template <class T>
MyClass<T> MyClass<T>::MyFunction(const MyClass& a, const MyClass& b)
{
MyClass<T> newObject(a.member, b.member2);
//...do stuff to the object
return newObject;
}
My fear with method 1 is that I am creating a new object in the heap and not ever destroying it. I am assigning all of its members to another instance of MyClass, but what happens to the first instance if I perform the operation a second time? Does the first "new" object I created on the stack just sit there?
The problem I have with method 2 is that the object is going out of scope before my assignment operation overload is called. I looked into copy constructors and move constructors. Unfortunately, I do not see those being called in this situation (probably a major lack of experience here).
For completion, here is the remaining portions of relevant code.
template <class T>
void MyClass<T>::operator = (const MyClass& b)
{
if (this == &b) { return; }
DeleteData();
//just calls the same member removal operation as the destructor
//to clear everything out to receive new data
//...simple member copying stuff
}
template<class T>
inline void MyClass<T>::DeleteData()
{
//call delete on members which need to be deleted manually
}
template <class T>
MyClass<T>::~MyClass()
{
DeleteData();
}
template <class T>
MyClass<T>::MyClass(const T member, const T member2)
{
//member initialization operations
}
EXAMPLE USAGE:
MyClass<T> myObj1(member1, member 2);
MyClass<T> myObj2(member1, member 2);
MyClass<T> myObj3 = *(MyClass<T>::MyFunction(myObj1, myObj2));
TL;DR Question:
When using new
inside a function to return a class*
instead of a class
is the created object destroyed when it no longer has any references?
If not, what is the best practice to return an object that is created in a method?
If so, is this method a bad practice?
I apologize if this has been answered before, I am new enough to C++ that my previous researched has not yielded me a satisfactory answer, likely due to me not knowing what I don't know.
IMPLEMENTED SOLUTION: (I also added a copy constructor. Without it, this solution still worked for the scope of the question, but there were some items beyond this question's scope that were fixed by one. Hopefully someone in the future might find this info useful)
//this is a static function of MyClass
template <class T>
std::unique_ptr<MyClass<T>> MyClass<T>::MyFunction(const MyClass& a, const MyClass& b)
{
std::unique_ptr<MyClass<T>> newObj = std::make_unique<MyClass<T>>(a.member, b.member2);
//...Be the factory...
return newObj ;
}
int main()
{
MyClass<T> myObj1(member1, member 2);
MyClass<T> myObj2(member1, member 2);
MyClass<T> myObj3 = *(MyClass<T>::MyFunction(myObj1, myObj2));
}