我们可以在 C++ 中使类复制构造函数成为虚拟的吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9889488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:22:38  来源:igfitidea点击:

Can we make a class copy constructor virtual in C++

c++constructorvirtual

提问by

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

我们可以在 C++ 中使类复制构造函数成为虚拟的吗?如何使用?

采纳答案by Luchian Grigore

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

不,你不能,构造函数不能是虚拟的。

C++03 - 12.1 Constructors

C++03 - 12.1 构造函数

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

4) 构造函数不应是virtual(10.3)或static(9.4)。[...]

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

如果您需要这样的东西,您可以在此处查找虚拟构造函数习语。

回答by Mankarse

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?

想想看。这样的构造函数有什么语义?

回答by George Davison

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. :)

但这并不意味着它不能用一点 C++ 魔法来完成。:)

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.

在某些情况下它非常有用,例如序列化非 POD 类。本示例创建了一个使用新布局工作的虚拟复制构造函数。

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);
    }
};

回答by timestee

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.

不。C++ 是静态类型语言,对于 C++ 编译器来说,以多态方式创建对象是没有意义的。编译器必须知道创建对象的类类型。换句话说,从 C++ 编译器的角度来看,要创建什么类型的对象是编译时的决定。如果我们将构造函数设为虚拟,编译器会标记一个错误。

回答by Priyanka Soni

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.

虚拟构造函数:- 不可能,因为 c++ 是静态类型语言,并且将构造函数创建为虚拟的,因此编译器将无法决定它是什么类型的对象,并且由于 virtual 关键字而将整个过程留给运行时。编译器必须知道创建对象的类类型。换句话说,从 C++ 编译器的角度来看,要创建什么类型的对象是编译时的决定。如果我们将构造函数设为虚拟,编译器会标记一个错误。

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.

虚拟复制构造函数:- 是的,可以考虑剪贴板应用程序。剪贴板可以保存不同类型的对象,并从现有对象复制对象,将它们粘贴到应用程序画布上。同样,要复制什么类型的对象是运行时决定。虚拟复制构造函数填补了这里的空白。

回答by Priyanka Soni

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

永远,这在 C++ 中是不可能的。