c++运算符必须是非静态成员函数

发布于 2024-11-17 03:22:44 字数 470 浏览 5 评论 0原文

可能的重复:
什么是“operator = 必须是非-”静态成员”是什么意思? (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛滥成性 2024-11-24 03:22:44

不,没有,这是标准第 13.5.3.1 段规定的:

赋值运算符应为
由非静态成员实现
函数只有一个参数。

No there is not, this is mandated by the standard, paragraph 13.5.3.1:

An assignment operator shall be
implemented by a non-static member
function with exactly one parameter.

注定孤独终老 2024-11-24 03:22:44

没有,赋值运算符需要声明为成员(基本原理是,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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文