小编Hug*_*gus的帖子

内部和外部重载C++运算符之间的区别

我有以下代码无法编译,抱怨+ =运算符不存在.+ =运算符在此处声明在A类之外.

template < typename _T >
class A {
public:
    operator _T () const { return 42 ; }
};

template <typename _T >
A< _T > & operator += ( A< _T > & l, _T r ) { return l ; }


int main() {
    A< int > e, f ;
    e += f ;
    return 0 ;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我在类A中实现运算符,代码编译并工作:

template < typename _T >
class A {
public:
    operator _T () const { return 42 …
Run Code Online (Sandbox Code Playgroud)

c++ templates class operator-overloading

7
推荐指数
1
解决办法
240
查看次数

标签 统计

c++ ×1

class ×1

operator-overloading ×1

templates ×1