C ++赋值运算符是否可以是自由函数? [英] Can C++ assignment operators be free functions?

查看:213
本文介绍了C ++赋值运算符是否可以是自由函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试这样:

Foo & operator=(Foo & to, const Bar &from);

但我收到此错误:

E2239 'operator =(Foo &, const Bar &)' must be a member function

有没有限制哪些操作符可以/不能定义为自由函数,如果是,为什么?

Are there limitations on which operators can/cannot be defined as Free Functions, and if so, why?

推荐答案

赋值运算符必须是非静态成员函数,并且必须只有一个参数:

The assignment operator must be a non-static member function and must have exactly one parameter:


(C ++ 03 13.5.3 / 1)的非静态成员函数实现。

An assignment operator shall be implemented by a non-static member function with exactly one parameter (C++03 13.5.3/1).

operator() operator [] operator-> 还必须实现为非静态成员函数。

operator(), operator[], and operator-> must also be implemented as non-static member functions.

特定类别运算符新操作符delete 和其变体)必须实现为静态成员函数(注意,这些是隐式静态的,即使它们不是用 static 关键字声明的)。

Class-specific operator new and operator delete (and variants thereof) must be implemented as static member functions (note that these are implicitly static, even if they are not declared with the static keyword).

这篇关于C ++赋值运算符是否可以是自由函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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