I recently needed to use a list of different derived classes, as one does quite often, and found myself in situation where I had already researched a ton (I know the principles but just wanted to see how people normally do it), but couldn’t find a single satisfying solution.
The problem goes as follows:
You want an object that can hold any derived class of a base class, so an owning base class pointer, though as a c++ programmer you do not want to deal with raw new and delete outside of their respective wrapper classes. You decide you’d use a smart pointer (mainly std::unique_ptr) like everyone suggests, but then find out that it is non copyable which is a functionality you also really need. std::shared_ptr instead is copyable, but you don’t want to duplicate just the base pointer, but the (derived) object it points to too (perform a deep copy). A new pointer to a new object in memory with the same values. What to do? After a lot of research you find out about the clone pattern, the use of something called CRTP, and some other (rather unconvenient) ways. Implementing a clone method in every base and derived class you may want to use seems kind of redundant, since it always does the same. Creating a new factory-like class for CRTP also seems kind of odd, since you know the type of the currently stored derived through the set method template (see code). You would just need to save it and cast the base pointer to whatever this template was at the point of setting your pointer in the copy constructor.
This brings me to the core question and as to why no one has brought up a simple and straight forward solution to this. I mean you just need to store a derived type of base dynamically, right? After some hours of further research and thinking myself, I think I found such a clean and simple solution, though I am not sure why no one proposed things like this anywhere or why I just could not find them no matter what I searched for. Are there any drawbacks to using this or is there anything I’ve missed? To me this seems like one of a few optimal ways of handling all this.
I basically just used a std::function which is dynamically overwritten whenever the pointer is set to a new derived to return a new object of the (just then) known derived type. For what I’ve tested it works perfectly fine and as expected, but what do you think of it? And have you found implementations somewhere or even have your own?
Here’s the implementation I have:
(Sure there are still some things to add like a direct assignment operator overload and stuff but for now it’s okay like this.)
template <typename BaseType>
class PolyPtr {
private:
BaseType* basePtr;
std::function<BaseType*()> cloneFunc;
public:
PolyPtr() {
basePtr = nullptr;
}
PolyPtr(const PolyPtr &pCopyFrom) {
basePtr = pCopyFrom.cloneFunc();
}
~PolyPtr() {
clear();
}
BaseType* get() {
return basePtr;
}
template <typename DerivedType>
DerivedType* getDerived() {
DerivedType* derivedPtr = dynamic_cast<DerivedType*>(basePtr);
return (derivedPtr ? derivedPtr : nullptr);
}
template <typename DerivedType>
void set(DerivedType pNewObject) {
clear();
basePtr = new DerivedType(pNewObject);
cloneFunc = [basePtr = this->basePtr]() -> DerivedType* {return new DerivedType(*static_cast<DerivedType*>(basePtr));};
}
void clear() {
delete basePtr;
basePtr = nullptr;
cloneFunc = []() -> BaseType* {return nullptr;};
}
};
UniCode is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1