理解虚拟拷贝构造函数 [英] understanding virtual copy constructors

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

问题描述

我无法理解我在用来学习C ++的书中的代码真正发生了什么。这里是代码:

  class Base 
{
public:
Base(){} ;
virtual〜Base(){};

virtual Base * Clone(){return new Base(* this);}
};

class Derived
{
public:
Derived(){};
virtual〜Derived(){};

virtual Base * Clone(){return new Derived(* this);}
};

因此在这个 Clone()理解该函数返回一个指向Base类对象的指针。我不明白是什么在该功能内发生。当我以前在 int * pInt = new int 中使用 new 时, c $ c> new 基本上在空闲存储器上为整数分配足够的内存,然后返回该地址,将地址应用于指针 pInt 。使用同样的逻辑,我试图理解代码的 new Derived(* this)部分。所以,我认为它是在一个Derived类对象的自由存储上分配足够的内存,并返回地址,然后由函数 Clone()返回。 p>

为什么,通过构造函数,如果这是一个构造函数,它通过 * this 我理解 * this 意味着它传递任何被克隆的对象的地址,但我不明白 class_name(address_of_an_object) new 函数的上下文中。



有人可以解释该部分发生了什么吗?



提前感谢。

解决方案

误解在这里:


<$ c $


在现实中,这个是被克隆的对象的地址,但 * this (注意星号) >解除引用该地址。所以 * this 是类型 Derived& ,它是对被克隆对象的引用,而不是它的地址。 / p>

因此,调用 new Derived(* this)意味着在动态分配空间之后c> new ),新空间由复制构造函数 Derived(const Derived&)初始化,实际上是用户定义的,因此使用(编译器生成的)默认版本的拷贝构造函数。






new 的语义:如果 C 是类,则

  new C; 

为类型 C 然后调用 C 的构造函数来初始化该空格。这是 new 语义的一部分:它总是调用构造函数来初始化新分配的空间。



您呼叫

  new C(a,b,c); 

有一些参数 a b c ,则 new 将调用 C ,它接受这三个参数。



现在在特殊情况下你调用

  new C(a); 

使用参数 a 类型 C& new 将一如既往地调用适当的构造函数。适当的构造函数是 C(C&)(如果已定义)或 C(const C&)由编译器自动定义的复制构造函数)。


I'm having trouble understanding what's really happening with the code in a book I'm using to learn C++. Here's the code:

class Base
{
    public:
        Base() {};
        virtual ~Base() {};

        virtual Base* Clone() {return new Base(*this);}
};

class Derived
{
    public:
        Derived() {};
        virtual ~Derived() {};

        virtual Base* Clone() {return new Derived(*this);}
};

So in this Clone() function I understand that the function returns a pointer to a Base class object. What I don't understand is what's happening within that function. When I've previously used new as in int *pInt = new int, I was under the impression that new essentially allocates enough memory on the free store for an integer, then returns that address, applying the address to the pointer pInt. With that same logic I'm trying to understand the new Derived(*this) portion of the code. So, I think it is allocating enough memory on the free store for a Derived class object, and returning the address, which is then returned by the function Clone().

Why, though, does it pass *this through the constructor, if that is a constructor? I understand *this means its passing the address of whatever object is being cloned, but I don't understand the syntax of class_name(address_of_an_object) in the context of the new function.

Could someone please explain what's happening in that portion?

Thanks in advance.

解决方案

The misunderstanding is here:

*this means its passing the address of whatever object is being cloned

In reality, this is the address of the object that is being cloned, but *this (note the asterisk) is the result of dereferencing that address. So *this is of type Derived &, it's a reference to the object being cloned, not its address.

Therefore, calling new Derived(*this) means that after dynamically allocating space (which is what new does), the new space is initialised by the copy constructor Derived(const Derived &), which in this case hasn't actually been user-defined, so the (compiler-generated) default version of the copy constructor is used.


To clarify the semantics of new: If C is a class, then

new C;

allocates enough space for an object of type C and then calls the constructor of C to initialise that space. This is part of the semantics of new: It always calls the constructor to initialise the newly allocated space.

When you call

new C(a,b,c);

with some arguments a, b and c, then new will call a constructor of C that takes these three arguments. If no such constructor has been defined, you'll get a compiler error.

Now in the special case where you call

new C(a);

with an argument a that is itself of type C&, new will, as always, call the appropriate constructor. The appropriate constructor is either C(C &) (if defined), or C(const C&) (copy-constructor auto-defined by the compiler).

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

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