Can we make a class copy constructor virtual in C+

2019-01-25 02:24发布

Can we make a class copy constructor virtual in C++? How to use?

6条回答
虎瘦雄心在
2楼-- · 2019-01-25 03:05

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.

查看更多
相关推荐>>
3楼-- · 2019-01-25 03:13

No you can't, constructors can't be virtual.

C++03 - 12.1 Constructors

4) A constructor shall not be virtual (10.3) or static (9.4). [...]

If you need something like this, you can look up the virtual constructor idiom here.

查看更多
对你真心纯属浪费
4楼-- · 2019-01-25 03:21

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);
    }
};
查看更多
乱世女痞
5楼-- · 2019-01-25 03:24

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?

查看更多
Deceive 欺骗
6楼-- · 2019-01-25 03:27

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.

查看更多
乱世女痞
7楼-- · 2019-01-25 03:29

Never, it won't possible in C++.

查看更多
登录 后发表回答