簡體   English   中英

運算符在c ++中重載

[英]Operators overloading in c++

我最近發現了在c ++中重載運算符。 如果你想在類中重載一個操作符,並且我們想用它創建一個新的對象,那就是我們定義的其他人可以做的

NameOfClass operator+(const NameOfClass& b){
  {
         NameOfClass tmp;
         tmp.length = this->length + b.length;
         tmp.breadth = this->breadth + b.breadth;
         tmp.height = this->height + b.height;
         return  tmp;

}

我無法弄清楚我是否在此之前定義了2個對象。 例如

NameOfClass one(length,breadth,height);
NameOfClass two(length,breadth,height);

我設定了他們的屬性。 但是怎么做

NameOfClass three=one+two;

設置“三”的屬性? 是否將一個和兩個視為“+”重載運算符的參數。 功能清楚地說明了

tmp.length = this->length + b.length;

但是這個 - >長度應該是未定義的,而b.length是私有的。 它是如何混合在一起的? 或者它被視為方法所以one+two = +是一個方法,兩個是作為參數傳遞,這意味着this-> length是指“一個”對象的長度? 使用tutorialspoint中的示例。

#include <iostream>
using namespace std;

class Box
{
   public:

      double getVolume(void)
      {
         return length * breadth * height;
      }
      void setLength( double len )
      {
          length = len;
      }

      void setBreadth( double bre )
      {
          breadth = bre;
      }

      void setHeight( double hei )
      {
          height = hei;
      }
      // Overload + operator to add two Box objects.
      Box operator+(const Box& b)
      {
         Box box;
         box.length = this->length + b.length;
         box.breadth = this->breadth + b.breadth;
         box.height = this->height + b.height;
         return box;
      }
   private:
      double length;      // Length of a box
      double breadth;     // Breadth of a box
      double height;      // Height of a box
};
// Main function for the program
int main( )
{
   Box Box1;                // Declare Box1 of type Box
   Box Box2;                // Declare Box2 of type Box
   Box Box3;                // Declare Box3 of type Box
   double volume = 0.0;     // Store the volume of a box here

   // box 1 specification
   Box1.setLength(6.0); 
   Box1.setBreadth(7.0); 
   Box1.setHeight(5.0);

   // box 2 specification
   Box2.setLength(12.0); 
   Box2.setBreadth(13.0); 
   Box2.setHeight(10.0);

   // volume of box 1
   volume = Box1.getVolume();
   cout << "Volume of Box1 : " << volume <<endl;

   // volume of box 2
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;

   // Add two object as follows:
   Box3 = Box1 + Box2;

   // volume of box 3
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;

   return 0;
}

1。

如何

 NameOfClass three=one+two; 

設置“三”的屬性?

NameOfClass three=one+two; 將被解釋為NameOfClass three = one.operator+(two); three將從NameOfClass::operator=()的返回值構造復制/移動。

2。

 tmp.length = this->length + b.length; 

但是這個 - >長度應該是未定義的,而b.length是私有的。 它是如何混合在一起的?

你是什​​么意思“這個 - >長度應該是不確定的”? 下面this == &oneb == two

b.lengthprivate無所謂因為operator+是成員函數,它可以訪問私有成員。

NameOfClass three = one + two; 將使用編譯器生成的復制構造函數,以便從匿名臨時 one + two構造three one + two是你實現的one.operator+(two)語法糖。

更聰明的編譯器可能使用編譯器生成的移動構造函數,因為很明顯匿名臨時不能被其他任何東西使用。

您可以自己實現這些構造函數,也可以依賴編譯器生成復制(或移動 )成員數據的構造函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM