开发者

c++ operator must be non static member function [duplicate]

开发者 https://www.devze.com 2023-03-15 21:23 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: What does “operator = must be a non-static member” mean? (C++)
This question already has answers here: Closed 11 years ago.

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;
    re开发者_如何学Cturn 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


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号