Can we make a class copy constructor virtual in C++? How to use?
-
4Assuming that you could... which type should the constructor call be dispatched to? – David Rodríguez - dribeas Mar 27 '12 at 12:39
-
1Duplicate of http://stackoverflow.com/questions/733360/why-do-we-not-have-a-virtual-constructor-in-c – Tadeusz Kopec for Ukraine Mar 27 '12 at 12:40
-
@DavidRodríguez-dribeas: That is a good point. I think it should be an answer, as it also explains the rationale – Nawaz Mar 27 '12 at 12:41
-
@DavidRodríguez-dribeas: the c++faq link in Luchian's answer provides an answer. I wouldn't mind having it directly in the language instead of having to provide create() and clone() (say in the next standard). – stefaanv Mar 27 '12 at 12:49
-
1@stefaanv, nawaz: I know the idiom, the comment is meant to make you think that the constructor is applied to an object that is not yet created (at this point it is only allocated memory), and that dispatch in C++ is applied on the object of which the method is being called (at this point just a memory block). The idiom reverses the order, and uses virtual dispatch on the *source* object, rather than the destination, which is a valid object. The intention was making the user think on what was being asked. As of the idiom becoming part of the standard, I would not bet on it. – David Rodríguez - dribeas Mar 27 '12 at 13:29
-
@DavidRodríguez-dribeas: I already answered this to a previous question (http://stackoverflow.com/questions/1108008/any-ideas-for-c1y/1108145#1108145), but I'm not holding my breath... – stefaanv Mar 27 '12 at 14:09
6 Answers
No you can't, constructors can't be virtual.
C++03 - 12.1 Constructors
4) A constructor shall not be
virtual
(10.3) orstatic
(9.4). [...]
If you need something like this, you can look up the virtual constructor idiom here.

- 253,575
- 64
- 457
- 625
-
-
Although solving it with virtual methods can be in most cases okay, it is important to mention, that **these are not constructors**, it is an abstract factory. – peterh Oct 24 '18 at 11:38
No you cannot.
Furthermore, the whole concept does not make sense. Virtual functions are functions that are dispatched based on the value of an object (the dynamic type of the object). When a constructor is called, the object does not yet have a value (because it has not yet been constructed). Therefore, no virtual dispatch can possibly occur.
Think about it. What semantics would such a constructor have?

- 39,818
- 11
- 97
- 141
-
Thank you for explaining why NOT possible to create a virtual constructor. – DJJ Apr 02 '20 at 17:34
You cannot because the memory is allocated before the constructor is called based on the size of the new type not the copy operand. And if it did work it would be a special case that inverted polymorphism for a number of language constructs.
But that doesn't mean it can't be done with a little C++ magic. :)
There are couple cases where it is incredibly helpful, Serializing non-POD classes for instance. This example creates a virtual copy constructor that works using placement new.
Warning: This is an example that may help some users with specific problems. Do not do this in general purpose code. It will crash if the memory allocated for the new class is smaller than the derived class. The best (and only) safe way to use this is if you are managing your own class memory and using placement new.
class VirtualBase
{
public:
VirtualBase() {}
virtual ~VirtualBase() {}
VirtualBase(const VirtualBase& copy)
{
copy.VirtualPlacementCopyConstructor(this);
}
virtual void VirtualPlacementCopyConstructor(void*) const {}
};
class Derived :: public VirtualBase
{
public:
...
Derived(const Derived& copy) : ... don't call baseclass and make an infinite loop
{
}
protected:
void VirtualPlacementCopyConstructor(void* place) const
{
new (place) Derived(*this);
}
};

- 103
- 1
- 9
-
This won't compile, even if you fix it to get it to compile, the implementation is rather thoroughly broken. All members of `Derived` will suffer from double initialization. This might happen to work with some simple types, but clearly results in undefined behavior. The "constructor" would need to be `void VirtualPlacementCopyConstructor(VirtualBase* place) const { auto d = dynamic_cast
(place); if (d) { d->~Derived(); new (d) Derived(*this); } }` – Kuba hasn't forgotten Monica May 17 '17 at 21:21 -
It should be now obvious what the problem is: the `VirtualPlacementCopyConstructor` is invoked way too early, when the type of the copied-into class is not `Derived` yet. Thus the destructor won't do the right thing either, and you get undefined behavior aplenty. If you replace `dynamic_cast` with `static_cast`, things will work for POD types, and that's about it. As soon as you add e.g. a `std::string` member, stuff will break (worse yet: it won't always break, so you'll be pulling your hair out). – Kuba hasn't forgotten Monica May 17 '17 at 21:26
No. C++ being static typed language, it is meaningless to the C++ compiler to create an object polymorphically. The compiler must be aware of the class type to create the object. In other words, what type of object to be created is a compile time decision from C++ compiler perspective. If we make constructor virtual, compiler flags an error.

- 1,086
- 12
- 36
Never, it won't possible in C++.
Yes you can create virtual copy constructor but you can not create virtual constructor.
Reason:
Virtual Constructor:- Not Possible because c++ is static type language and create constructor as a virtual so compiler won't be able to decide what type of object it and leave the whole process for run time because of virtual keyword. The compiler must be aware of the class type to create the object. In other words, what type of object to be created is a compile time decision from C++ compiler perspective. If we make constructor virtual, compiler flags an error.
Virtual Copy constructor:- Yes Possible, consider clip board application. A clip board can hold different type of objects, and copy objects from existing objects, pastes them on application canvas. Again, what type of object to be copied is a runtime decision. Virtual copy constructor fills the gap here.
-
Whether the language permits using virtual copy constructor is different from whether the concept should be possible. – MAChitgarha Apr 11 '21 at 19:56