复制构造函数需要调用依赖于对象的方法,但构造函数不能是虚拟的 [英] Copy constructor needs to call a method that depends on the object, but constructor can't be virtual

查看:78
本文介绍了复制构造函数需要调用依赖于对象的方法,但构造函数不能是虚拟的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个继承类的抽象基类。在这两个类中,我定义了一个由构造函数使用的虚方法。现在我需要创建一个复制构造函数,但我不能将复制构造函数声明为虚拟,但我想要在其中的方法调用依赖于作为参数提供的对象的类型。我如何解决这个问题?

I have an abstract base class with two inherited classes. In both these classes I define a virtual method that is used by the constructor. Now I need to create a copy constructor, but I can not declare the copy constructor as virtual, but I want the method call inside it to be dependent on the type of object that is fed as argument. How can I solve this?

现在我使用基类定义参数的类型,这不工作,因为我在复制构造函数中调用的方法没有在基类中定义。

Right now I use the base class to define the type of argument, this doesn't work since the method I call inside the copy constructor is not defined in the base class.

基本上我需要复制构造函数为虚拟。

Basically I need the copy constructor to be virtual.

推荐答案


  1. 请谨慎在基类构造函数中调用虚拟方法。 li>
  2. 您可能需要使用克隆-method

  1. Be careful about calling virtual methods in base class constructors.
  2. You may want to use a clone-method.

这篇关于复制构造函数需要调用依赖于对象的方法,但构造函数不能是虚拟的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆