本文介绍了派生类中的复制控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Effective C ++ 3rd Edition项目6,P39


---------------------------------- ---------------------


class Uncopyable {

protected://允许施工

Uncopyable(){} //和销毁

~Uncopyable(){} //派生对象......

private:

Uncopyable(const Uncopyable&); // ...但是防止复制

Uncopyable& operator =(const Uncopyable&);

};


为了防止HomeForSale对象被复制,我们现在要做的就是

继承自Uncopyable:


class HomeForSale:private Uncopyable {// class不再

... //

声明复制ctor或

}; //复制

分配。运营商


------------------------------------- ------------------


我理解上课的副本和副本分配

HomeForSale如果需要,将由编译器隐式生成。在HomeForSale的副本中,首先它将隐式调用

Uncopyable的默认ctor。至于副本分配,它不会在基类中调用

对应物。

但是为什么作者说:


------------------------------------------------ -------

这样可行,因为编译器会尝试生成一个复制构造函数

和一个复制赋值运算符(如果有人) - 甚至是成员或朋友

函数 - 尝试复制HomeForSale对象。正如第12项所解释的那样,这些函数的编译器生成的版本将尝试调用它们的基类对应的

,并且这些调用将被拒绝,

因为复制操作在基类中是私有的。

---------------------------- ---------------------------


我在VC7.0中尝试过它确实来了用编译错误代替

链接错误。


提前致谢!

"Effective C++ 3rd Edition" Item 6, P39

-------------------------------------------------------

class Uncopyable {
protected: // allow construction
Uncopyable() {} // and destruction of
~Uncopyable() {} // derived objects...
private:
Uncopyable(const Uncopyable&); // ...but prevent copying
Uncopyable& operator=(const Uncopyable&);
};

To keep HomeForSale objects from being copied, all we have to do now is
inherit from Uncopyable:

class HomeForSale: private Uncopyable { // class no longer
... //
declares copy ctor or
}; // copy
assign. operator

-------------------------------------------------------

I understand that the copy ctor and copy assignment of class
HomeForSale will be implicitly generated by the compiler if needed. In
the copy ctor of HomeForSale, first it will call the default ctor of
Uncopyable implicitly. As for copy assignment, it won''t call the
counterpart in the base class.
But why the author says:

-------------------------------------------------------
This works, because compilers will try to generate a copy constructor
and a copy assignment operator if anybody - even a member or friend
function - tries to copy a HomeForSale object. As Item 12 explains,
the compiler-generated versions of these functions will try to call
their base class counterparts, and those calls will be rejected,
because the copying operations are private in the base class.
-------------------------------------------------------

I tried it in VC7.0 and it did come out with a compiled error instead
of a link error.

Thanks in advance!

推荐答案



对。那么你的问题是什么?它出现编译错误

而不是链接错误?这就是发生的事情。我没有看到你引用的什么

说它会产生链接错误。你在问什么?

Right. So what''s your question? That it came out with compile error
instead of link error? That''s what happens. I don''t see anything in what
you quoted saying it would generate a link error. What are you asking?



$ b $那么?编译错误正是您期望得到的,对吧?作者说

没有任何关于链接错误的信息(如果某人'和'b $ ba的朋友,那么你会得到链接错误_and_ Derived(或者是一个和一个的成员)

其他的朋友)试图复制对象)


- 西尔维斯特

So? A compile error is just what you''d expect to get, right? The author says
nothing about link errors (you will get link errors when someone that''s both
a friend of Base _and_ Derived (or is a member of one and a friend of the
other) tries to copy the object)

- Sylvester




那么?编译错误正是您期望得到的,对吧?作者说

没有任何关于链接错误的信息(如果某人'和'b $ ba的朋友,那么你会得到链接错误_and_ Derived(或者是一个和一个的成员)

的其他朋友试图复制对象)


- 西尔维斯特


So? A compile error is just what you''d expect to get, right? The author says
nothing about link errors (you will get link errors when someone that''s both
a friend of Base _and_ Derived (or is a member of one and a friend of the
other) tries to copy the object)

- Sylvester


这篇关于派生类中的复制控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 18:55