Well, I need to return a pointer to an instance of a class that will be created inside a function. Is this appropriate?
this is example code:
template <typename T>
ImplicatedMembershipFunction<T>*
TriangularMF<T>::minImplicate(const T &constantSet) const
{
static ImplicatedType* resultingSet = new ImplicatedType();
// do something to generate resultingSet...
return resultingSet;
}
I want to return pointers, because need to have subclasses of a base class in a container. In the above code ImplicatedType is a class defined in TriangularMF<T> and derived from ImplicatedMembershipFunction<T>. There will be various template classes like TriangularMF that the have a nested class derived from ImplicatedMembershipFunction<T>, I need to treat with them in same way. For example, outside the library, I may want to do something like :
TriangularMF<double> trmf(0,1,2);
TrapesoidalMF<double> trpmf(0,1,3,2); // a class like TriangularMF but
// ImplicatedType is different
ImplicatedMembershipFunction<double>* itrmf = trmf.implicate(0.6);
ImplicatedMembershipFunction<double>* itrpmf = trpmf.implicate(0.6); // same as above.
// use them in the same way:
vector<ImplicatedMembershipFunction<double>*> vec;
vec.push_back(itrmf);
vec.push_back(itrpmf);
The reason that I don’t want to use C++11 features like move semantics or std::shared_ptr is that I don’t like to force my teammates to install newer versions of g++ on their computers. I can’t give them a compiled version of the library, because it’s heavily templated.
EDIT
The library is going to be threaded. Especially, the TriangularMF<T>::minImplicate will run in multiple threads at same time. So, making the minImplicate a mutal task, makes no sense for the performance.
Returning a pointer is not itself the issue, but you have to define a clean “policy” about whoi creates and who destroy.
In your code, you define a static pointer that is initialized with a
newobject the very first time its (pointer) definition is encountered.The pointer itself will be destroyed just after
main()will return, but what about the object it points to?If you let something else to take care of the deletion, your function will continue to return that pointer even if the object is no more there. If you let it there, it will be killed out at the end of the program (not a “dangerous” leak, since it is just one object, but what about if its destructor has to take some sensible actions?)
You have most likely to declare, not a static pointer, but a static OBJECT, and return … its address or its reference.
In that way the object is granted to exist up to program termination and to be properly destroyed after
main()returns.Note that I eliminated your “do something to …” since it will be executed every time (not just the very first) To initialize
ImplicatedType, you had better to rely on the constructor.Or, if you cannot construct it in one shot, do something like
If you are in a multithreading situation, you also need a static mutex an lock it before
if(init), unlocking at return.