Advertisement

C++助教篇 Week2 找代码错误

阅读量:

为何以下有关<<运算符的重载操作会导致错误呢?

复制代码
    class MyClass {
    public:
    MyClass() = default;
    friend ostream& operator<<(ostream& out, MyClass& obj);
    };
    
    int main(int argc, const char** argv) {
    MyClass a, b;
    std::cout << a + b << std::endl;  // error
    
    MyClass c;
    c = a + b;
    std::cout << c << std::endl;
    
    system("pause");
    return 0;
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    

尽管C++语言本身并不具备连续比较的功能,然而在实际运行过程中,以下代码依然能够输出True的结果。

复制代码
    if (2 < 9 < 3) {

全部评论 (0)

还没有任何评论哟~