虚拟副本构造函数 [英] virtual copy constructor

查看:89
本文介绍了虚拟副本构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在绞尽脑汁想要今天整天弄明白......现在凌晨2点。请有人向我解释以下内容:我知道每个类中都有虚拟克隆方法,以方便虚拟拷贝构造函数的使用,因为你不能直接声明

*虚拟哺乳动物(const Mammal& rhs) );* 以这种方式。但是,我无法理解克隆方法正在做什么。让我们举例说,在提示符下,我输入2.然后switch语句在free store上创建一个Cat对象并将其分配给ptr。然后将ptr分配给theArray [i]。该循环继续,直到for循环结束。然后它执行下一个for循环,其中存储在theArray [i]中的地址访问speak方法。你看到喵在输出中。在下一步中,它将访问clone方法,并调用Cat拷贝构造函数和Dog拷贝构造函数。 这怎么可能?这里发生了什么步骤? Cat的克隆方法如何调用复制构造函数?我可能不理解* this的基本原理,因为我怀疑它是负责调用复制构造函数的。

I have been plunging my brains trying to figure this out all day today...Its 2 am now. Please, someone explain the following to me: I know the virtual clone method is there in each class to facilitate the use of virtual copy constructors since you cannot directly declare
*virtual Mammal (const Mammal & rhs);* in this manner. However, I cannot understand what the clone method is doing. Lets say for example, at the prompt, I enter 2. Then the switch statement creates a Cat object on the free store and assigns it to ptr. Then ptr is assigned to theArray[i]. This cycle continues until the for loop ends. Then it executes the next for loop with the addresses stored in theArray[i] accessing the speak method. you see "Meow" in the output. In the next step it access the clone method and the both the Cat copy constructor and the Dog copy constructor is called. How is this possible? And what are the steps taking place here? How does the Cat''s clone method call the copy constructors? It is possible that I am not understanding the fundamentals of *this as I suspect that this is responsible for calling the copy constructor.

展开 | 选择 | Wrap | 行号

推荐答案

构造函数 - Dog :: Dog(const Dog& rhs)

代码 - 返回新的Dog(* this);

''这个''变量这里是指向Dog *类型的类实例的指针,然后它被解除引用类型Dog&并传递给构造函数。它的工作方式与它们的工作方式相同
constructor - Dog::Dog(const Dog & rhs)
code - return new Dog(*this);
''this'' variable here is a pointer to the class instance, of the type Dog*, then it is dereferenced to the type Dog& and passed to constructor. It works the same way as if they were
展开 | 选择 | Wrap | 行号


克隆是副本。


在C ++中,您可以使用复制构造函数创建克隆。


在C ++以外的语言中,如果没有复制构造函数,则需要编写克隆方法制作副本。
A clone is a copy.

In C++ you create a clone by using the copy constructor.

In languages other than C++ where there is notjhing like a copy constrcutor, you need to write a clone method to make the copy.


感谢您的回复。但我很抱歉,我还没有完整的画面。让我知道这是否正确:在虚拟功能中:


虚拟哺乳动物*克隆(){返回新狗(* this)}


它在免费商店中创建一个Dog对象,并将调用对象的内容分配给这个新创建的Dog。然后它返回一个指向哺乳动物的指针。然后这个指针存储在OtherArray [i]中,它将迭代显示所有说话方法。哺乳动物指针能够在Dog,Cat等中访问正确的说话方法,因为Mammal中的说话方法是虚拟的。到目前为止我是否正确?现在,问题是:在这个过程中是什么时候调用复制构造函数?什么是复制构造函数呢?在继续讨论多态性之前,我真的认为我应该理解这一点。
Thanks for the responses. But I am sorry, i''m still not getting the complete picture. Let me know if this is correct: in the virtual function:

virtual Mammal * Clone(){return new Dog(*this)}

It creates a Dog object on the free store and assigns the calling object''s contents to this newly created Dog. Then it returns a pointer to a Mammal. Then this pointer is stored in OtherArray[i] which will iterate to display all the speak methods. A mammal pointer is able to access the correct speak methods in Dog, Cat, etc because the speak method in Mammal is virtual. Am I correct so far? Now, the question is: When in this process is the copy constructor called? And what is the copy constructor doing? I really think I should understand this before moving on to polymorphism.


这篇关于虚拟副本构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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