为什么不允许复制构造函数和赋值运算符? [英] Why copy constructor and assignment operator are disallowed?

查看:466
本文介绍了为什么不允许复制构造函数和赋值运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#undef GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
#define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName)    \
   TypeName(const TypeName&);                           \
   void operator=(const TypeName&)

来自google的代码。
为什么不允许复制构造函数和赋值运算符?

I'm, reading open source code from google. Why copy constructor and assignment operator are disallowed?

推荐答案

防止复制或分配类的实例。大多数类不应允许复制。例如,考虑一个BankAccount类 - 如果你正在为银行编写软件,如果你创建帐户的副本,然后对这些不同的副本应用积分和借记,他们不会太高兴。

To prevent instances of the class being copied or assigned. Most classes should not allow copying. Consider for example a BankAccount class - if you are writing software for a bank, they will not be too happy if you create copies of accounts and then apply credits and debits to those different copies.

这篇关于为什么不允许复制构造函数和赋值运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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