문제

In the following :

template<typename Derived>
class Base:
{
    inline Derived& operator=(const Base<Derived>& x);
}

Does this declaration erases the default copy assignment operator or do I have two operators :

inline Derived& operator=(const Base<Derived>& x); 
// (declared by me)

AND

inline Base<Derived>& operator=(const Base<Derived>& x); 
// (declared by the compiler)

In this case, when I call the function, how the compiler will get the right operator ?

도움이 되었습니까?

해결책

If you declare any method that can pass for an assignment operator:

XXX Foo::operator=(Foo&);
XXX Foo::operator=(Foo const&);
XXX Foo::operator=(Foo volatile&);
XXX Foo::operator=(Foo const volatile&);

then the compiler will not generate the default version Foo& operator=(Foo const&);.

Note that the return type is completely free, as for other methods. You could use void, bool, whatever really. It is just idiomatic (but not required) to return a reference to self in order to allow assignment chaining: a = b = c = 0; which itself stems from the guideline that overloaded operators should follow the semantics of their built-in counterparts.

다른 팁

Have you tried? Overloading by return type only would be a compile error, so my guess is that the one defined replaces the default one.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top