c++运算符必须是非静态成员函数
我正在尝试将operator= 方法编写为非成员,并带有如下两个参数:
template<class T>
T operator=(T & t, const myclass<T>& m)
{
t = m.val;
return t;
}
但我收到错误,operator= 必须是非静态成员。是否有编译器标志或某种方法来欺骗编译器让我运行它?
谢谢
Possible Duplicate:
What does “operator = must be a non-static member” mean? (C++)
I'm trying to write an operator= method as a non member, with 2 arguments like this:
template<class T>
T operator=(T & t, const myclass<T>& m)
{
t = m.val;
return t;
}
But I get the error that operator= must be a nonstatic member. Is there a compiler flag or some way to trick the compiler to let me run this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,没有,这是标准第 13.5.3.1 段规定的:
No there is not, this is mandated by the standard, paragraph 13.5.3.1:
没有,赋值运算符需要声明为成员(基本原理是,iirc,以防止您覆盖基元或库类型的赋值)。
There isn't, assignment operators need to be declared as members (The rationale is, iirc, to keep you from overriding assignment for primitive or library types).