Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a copy of an unknown concrete type in c++

Suppose we have the following class hierarchy:

class Base {
    ...
};

class Derived1 : public Base {
    ...
};

class Derived2 : public Base {
    ...
};

Given a Base* which could point to either a Derived1 or Derived2 object how can I make a copy of the actual object given that it's concrete type is unknown. I thought of defining copy constructors but I don't think this is possible without knowing the actual types involved. The only solution I can think of is defining a clone() method on each type in the hierarchy. Can anybody think of something more elegant?

like image 656
Nate Avatar asked Jan 27 '09 01:01

Nate


People also ask

What is copy constructor in C?

The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to − Initialize one object from another of the same type. Copy an object to pass it as an argument to a function.

What is copy constructor with example?

When Copy Constructor is called. Copy Constructor is called in the following scenarios: When we initialize the object with another existing object of the same class type. For example, Student s1 = s2, where Student is the class. When the object of the same class type is passed by value as an argument.

Can we make copy constructor in abstract class?

Yes you should. Rules of having your own implementations for copy constructor, copy assignment operator and destructor for a Class will apply to even an Abstract Class.


2 Answers

Unfortunately a virtual clone/copy pattern is your only real choice.

There are variations of this, but essentially they all come down to writing functions for each type that can return a new copy.

like image 74
Andrew Grant Avatar answered Sep 23 '22 13:09

Andrew Grant


You need to implement the virtual clone method on every object in the hierarchy. How else would your object of undefined type know how to copy itself?

In order to make this more obvious to people writing derived classes, you might think about making the clone method abstract in your base class as well, to force people to at least write something.

In several places for my code, I also test to make sure that the to_string() method is implemented correctly to serialize an object to a string. The specific test that I've used in my class to test clone and to_string simultaneously looks like this:

Base *obj1, *obj2;
# initialize obj1 in a reasonable manner
obj2 = obj1->clone();
assert( obj1->to_string() == obj2->to_string() );

This is testing both the clone method() and object serialization to strings (so it's not strictly a unit test), but it's a paradigm that simple to wrap in a loop around all of the objects in a factory to make sure that they're following some minimum standard of implementing clone() and to_string().

Edit: Updated code with correction from strager's comment. Thanks!

like image 42
James Thompson Avatar answered Sep 19 '22 13:09

James Thompson