KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have been struggling with this kind of problem for a long time, so I decided to ask here. class Base { virtual ~Base(); }; class Derived1 : public Base { ... }; class Derived2 : public Base { ... }; ... // Copies the instance of derived class pointed by the *base pointer Base* CreateCopy(Base* base); The method should return a dynamically created copy, or at least store the object on stack in some data structure to avoid "returning address of a temporary" problem. The naive approach to implement the above method would be using multiple typeid s or dynamic_cast s in a series of if-statements to check for each possible derived type and then use the new operator. Is there any other, better approach? P.S.: I know, that the this problem can be avoided using smart pointers, but I am interested in the minimalistic approach, without a bunch of libraries.
Tags (comma-separated)
Save Edits
Cancel