相关疑难解决方法(0)

强制某些运营商成为成员的理由

C++中有4个运算符可以重载但不能作为独立(即非成员,独立)函数重载.这些运营商是:

  • operator =
  • operator ()
  • operator ->
  • operator []

这个主题完全解释了禁止operator =成为非成员函数的理由.关于其他三个的任何想法?

c++ operator-overloading c++-faq

22
推荐指数
1
解决办法
1544
查看次数

C++运算符重载 - 从类转换

在将Windows代码移植到Linux时,我在GCC 4.2.3中遇到以下错误消息.(是的,我知道这是一个旧的版本,但我不能轻易升级.)

main.cpp:16: error: call of overloaded ‘list(MyClass&)’ is ambiguous
/usr/include/c++/4.2/bits/stl_list.h:495: note: candidates are: std::list<_Tp, _Alloc>::list(const std::list<_Tp, _Alloc>&) [with _Tp = unsigned char, _Alloc = std::allocator<unsigned char>]
/usr/include/c++/4.2/bits/stl_list.h:484: note:                 std::list<_Tp, _Alloc>::list(size_t, const _Tp&, const _Alloc&) [with _Tp = unsigned char, _Alloc = std::allocator<unsigned char>]
Run Code Online (Sandbox Code Playgroud)

我正在使用以下代码生成此错误.

#include <list>
class MyClass
    {
    public:
        MyClass(){}

        operator std::list<unsigned char>() const { std::list<unsigned char> a; return a; }
        operator unsigned char() const { unsigned char a; return a; }

    };

    int main()
    {
        MyClass a; …
Run Code Online (Sandbox Code Playgroud)

c++ gcc operator-overloading operators operator-keyword

2
推荐指数
1
解决办法
4003
查看次数