Item 6. Explicitly disallow the use of compile-generated functions you do not want.
本篇条目介绍了如何防止编译器调用其自动创建的函数(item5中提到的4种函数)。
class NonCom{
public:
.....
private:
NonCom(const NonCom&);
NonCom& operator=(const NonCom&);
...
}; If you inadvertently try to do call this function in a member or a friend function, the linker will complain. class noncopyable{
public:
noncopyable(){}
~noncopyable(){}
private:
noncopyable(const noncopyable&);
noncopyable& operator=(const noncopyable&);
};
class NonCom : noncopyable{
...
}; 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 NonCom object. As Item 12 (See 10.8) 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. 《Effective C++》读书笔之六 Item 6. Explicitly disallow the use of compile-generated functions
原文:http://blog.csdn.net/swagle/article/details/19969923