【条款6】Explicitly disallow the use of compiler-generated functions you do not want
2012-10-26 15:38
417 查看
我们都知道,如果不想让一个class具有某一功能,只要不声明具有该功能的函数成员即可。可是对于copy construct 和 copying assignment确不是这样的,因为即使你不声明这两个函数,编译器也会自动为你生成。所以如果你想让class不具有这个功能,必须要做点工作了。
方法是:
1.将copy construct和copying assignment声明为private;
2.不予实现。
这样做的目的是:
1.自己声明可以阻止编译器自动生成相应版本;
2.令其属性为private,就可以阻止人们调用它。
3.不予实现可以在member function和friend function调用copy函数时时,产生一个连接错误。
这样class就不会具有copy功能。c++ iostream 程序库中就是如此copying行为的,比如ios_base, basic_ios和sentry。
连接错误示例如下所示:
解决方案:声明一个专门为了阻止copying动作设计的base class
方法是:
1.将copy construct和copying assignment声明为private;
2.不予实现。
这样做的目的是:
1.自己声明可以阻止编译器自动生成相应版本;
2.令其属性为private,就可以阻止人们调用它。
3.不予实现可以在member function和friend function调用copy函数时时,产生一个连接错误。
这样class就不会具有copy功能。c++ iostream 程序库中就是如此copying行为的,比如ios_base, basic_ios和sentry。
class A{ public: A(){} ~A() {} private: A(const A&); A& operator=(const A&); };
int main() { A a; A b(a); return 0; }会出现编译错误:阻止了拷贝的发生
error C2248: 'A::A' : cannot access private member declared in class 'A'
连接错误示例如下所示:
class A{ friend A copyA(const A &temp) {return A(temp);} public: A(){} ~A(){} A mycopy(const A& temp){return A(temp);} private: A(const A&); A& operator=(const A&); };
int main() { A a; A b; copyA(a); b.mycopy(a); return 0; }运行后会报连接错误。如何将连接期错误移至编译期呢?
解决方案:声明一个专门为了阻止copying动作设计的base class
class Uncopyable{ protected: Uncopyable() {} ~Uncopyable() {} private: Uncopyable(const Uncopyable &); Uncopyable& operator=(const Uncopyable &); };
class A:private Uncopyable{ ... };这样,任何人(包括member and friend function)尝试拷贝A对象,编译器便会试着生成一个copy construct和copy assignment,这些编译器生成版会尝试调用base class的相应函数,但这些调用会被拒绝,因为base class的copy函数是private。
相关文章推荐
- 6. Explicitly disallow the use of compiler generated functions you do not want
- Item5: Explicitly disallow the use of compiler-generated functions you do not want--若不想使用编译器自动生成的函数,就该明确拒绝
- effective c++ 6: Explicitly disallow the use of compiler-generated functions you do not want
- Item 6: Explicitly disallow the use of compiler-generated functions you do not want(Effective C++)
- Item6:Explicitly disallow the use of compiler-generated functions you do not want.
- 条款6:若不想使用编译器自动生成的函数,就该明确拒绝(Explicity disallow the use of compiler-generated functions you do not want)
- 条款06:若不想使用编译器自动生成的函数,就该明确拒绝(Explicitly disallow the use of compiler-generated you do not want.)
- 《Effective C++》读书笔之六 Item 6. Explicitly disallow the use of compile-generated functions
- 【Datastage】Datastage在win10上安装报错:This Application requires one of the following versions of the .NET Framework:v1.1.4322 Do you want to install this .NET Framework version now?
- You are using the runtime-only build of Vue where the template compiler is not available. Either pre
- 遭遇开机自检问题:If you want to install linux default partition RAID driver,please do not use OPROM creation oporation
- Source Insight in Ubuntu+Windows+Samba:has been changed outside of the editor. Do you want to reload the file?
- Spring AOP 循环应用(This means that said other beans do not use the final version of the bean..)
- 'NSURL' is not implicitly convertible to 'URL'; did you mean to use 'as' to explicitly convert?异常
- 160 You want to use the automatic management of backup and recovery operations features for your dat
- ccah-500 第45题 You want to minimize the chance of data loss in your cluster. What should you do
- 关于“This file has been modified outside of the source editor. Do you want to reload it?”错误的处理
- Source insight上修改samba共享ubuntu上内核源程序文件出现"Confirm by typing ‘yes' below"、"has been changed outside of the editor. Do you want to
- nagios报错 It appears as though you do not have permission to view information for any of the hosts yo