宏禁止类复制和分配。 Google -vs- Qt [英] Macros to disallow class copy and assignment. Google -vs- Qt

查看:235
本文介绍了宏禁止类复制和分配。 Google -vs- Qt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了禁止复制或分配类,通常的做法是使复制构造函数
和赋值运算符私有。 Google和Qt都有宏,使这个简单和可见。
这些宏是:

To disallow copying or assigning a class it's common practice to make the copy constructor and assignment operator private. Both Google and Qt have macros to make this easy and visible. These macros are:

Google:

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

Qt:

#define Q_DISABLE_COPY(Class) \
  Class(const Class &); \     
  Class &operator=(const Class &);

问题:
为什么两个赋值运算符的签名不同?它似乎是Qt版本是正确的。
两者之间的实际区别是什么?

Questions: Why are the signatures of the two assignment operators different? It seems like the Qt version is correct. What is the practical difference between the two?

推荐答案

返回类型不是函数签名的一部分,因为它不参与重载解析。因此,当您尝试执行赋值时,无论是否使用返回类型,两个声明都将匹配。

It doesn't matter. The return type is not part of a function's signature, as it does not participate in overload resolution. So when you attempt to perform an assignment, both declarations will match, regardless of whether you use the return type.

由于这些宏中的整个点是函数永远不会被调用,一个返回 void 并不重要。

And since the entire point in these macros is that the functions will never get called, it doesn't matter that one returns void.

这篇关于宏禁止类复制和分配。 Google -vs- Qt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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