久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

類模板上的運算符重載

Operator overloading on class templates(類模板上的運算符重載)
本文介紹了類模板上的運算符重載的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我在為模板類定義一些運算符重載時遇到了一些問題.讓我們以這個假設的類為例.

I'm having some problems defining some operator overloads for template classes. Let's take this hypothetical class for example.

template <class T>
class MyClass {
  // ...
};

  • 運算符+=

    • operator+=

      // In MyClass.h
      MyClass<T>& operator+=(const MyClass<T>& classObj);
      
      
      // In MyClass.cpp
      template <class T>
      MyClass<T>& MyClass<T>::operator+=(const MyClass<T>& classObj) {
        // ...
        return *this;
      }
      

      導致此編譯器錯誤:

      no match for 'operator+=' in 'classObj2 += classObj1'
      

    • 運算符<<

    • operator<<

      // In MyClass.h
      friend std::ostream& operator<<(std::ostream& out, const MyClass<T>& classObj);
      
      
      // In MyClass.cpp
      template <class T>
      std::ostream& operator<<(std::ostream& out, const MyClass<T>& classObj) {
          // ...
          return out;
      }
      

      導致此編譯器警告:

      friend declaration 'std::ostream& operator<<(std::ostream&, const MyClass<T>&)' declares a non-template function
      

    • 我在這里做錯了什么?

      推薦答案

      // In MyClass.h
      MyClass<T>& operator+=(const MyClass<T>& classObj);
      
      
      // In MyClass.cpp
      template <class T>
      MyClass<T>& MyClass<T>::operator+=(const MyClass<T>& classObj) {
        // ...
        return *this;
      }
      

      這對模板無效.運算符的完整源代碼必須在使用它的所有翻譯單元中.這通常意味著代碼內聯在標頭中.

      This is invalid for templates. The full source code of the operator must be in all translation units that it is used in. This typically means that the code is inline in the header.

      從技術上講,根據標準,可以導出模板,但是很少有編譯器支持它.此外,如果模板在 MyClass.cpp 中為所有 T- 類型顯式實例化,您也可以執行上述操作,但實際上,這通常無視模板的要點.

      Technically, according to the Standard, it is possible to export templates, however very few compilers support it. In addition, you CAN also do the above if the template is explicitly instantiated in MyClass.cpp for all types that are T- but in reality, that normally defies the point of a template.

      更多我通讀了你的代碼,它需要一些工作,例如重載運算符 [].此外,通常,我會將維度作為模板參數的一部分,允許在編譯時捕獲 + 或 += 的失敗,并允許有意義地堆棧分配類型.您的異常類還需要從 std::exception 派生.然而,這些都不涉及編譯時錯誤,它們只是不是很好的代碼.

      More edit: I read through your code, and it needs some work, for example overloading operator[]. In addition, typically, I would make the dimensions part of the template parameters, allowing for the failure of + or += to be caught at compile-time, and allowing the type to be meaningfully stack allocated. Your exception class also needs to derive from std::exception. However, none of those involve compile-time errors, they're just not great code.

      這篇關于類模板上的運算符重載的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

      【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 中文字幕久久精品 | 成年人免费看 | 精品日韩一区二区 | 精品久久香蕉国产线看观看亚洲 | 午夜影院官网 | 国产高清视频在线观看 | 久久成人一区 | 亚洲人成人一区二区在线观看 | 久久精品免费 | 久久国产日韩 | av黄色网 | 欧美精品久久 | 国产精品欧美一区二区三区 | 岛国毛片在线观看 | 亚洲精品久久久蜜桃网站 | 一区二区三区国产精品 | 国产精品一区一区 | 黄色一级大片在线免费看产 | 日韩在线观看中文字幕 | 国产在线看片 | 亚洲a人 | 午夜精品久久久久久久99黑人 | 丝袜一区二区三区 | 米奇7777狠狠狠狠视频 | 久久国产精品久久久久久 | 欧美亚洲网站 | 久久久久久国产精品久久 | 亚洲欧美国产视频 | 在线播放国产一区二区三区 | 成人毛片视频免费 | 免费欧美 | 青青草这里只有精品 | www.操com | 天天干.com | 国产精品18久久久 | 欧美精品一区二区三区一线天视频 | 91在线观看视频 | 久久国产激情视频 | 国产高清亚洲 | 夜夜骑天天干 | 福利视频网站 |