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; }

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; },c++,c++11,operator-overloading,C++,C++11,Operator Overloading,我不知道在此之前是否定义了两个对象。e、 g NameOfClass one(length,breadth,height); NameOfClass two(length,breadth,height); 我设定了他们的属性。 但是怎么做呢 NameOfClass three=one+two; 设置“三”的属性?都被视为“+”重载运算符的参数。函数清楚地表示 tmp.length = this->length + b.length; 但是这个->长度应该是未定义的,而b.length

我不知道在此之前是否定义了两个对象。e、 g

NameOfClass one(length,breadth,height);
NameOfClass two(length,breadth,height);
我设定了他们的属性。 但是怎么做呢

NameOfClass three=one+two;
设置“三”的属性?都被视为“+”重载运算符的参数。函数清楚地表示

tmp.length = this->length + b.length;
但是这个->长度应该是未定义的,而b.length是私有的。它是如何混合在一起的? 还是将其作为方法,因此
one+two
=one+two的+is方法作为参数传递,这意味着该->长度引用“one”对象的长度? 使用教程中的示例

#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;
}
#包括
使用名称空间std;
类框
{
公众:
双卷(无效)
{
返回长度*宽度*高度;
}
空隙设置长度(双透镜)
{
长度=长度;
}
无效收进深度(双bre)
{
宽度=bre;
}
空隙设置高度(双高)
{
高度=高;
}
//重载+运算符以添加两个长方体对象。
接线盒操作员+(常数接线盒和b)
{
盒子;
box.length=this->length+b.length;
box.width=此->宽度+b.宽度;
box.height=this->height+b.height;
返回框;
}
私人:
双倍长度;//长方体的长度
双宽度;//盒子的宽度
双倍高度;//盒子的高度
};
//程序的主要功能
int main()
{
Box Box1;//声明Box类型的Box1
Box Box2;//声明Box类型的Box2
Box Box3;//声明Box类型的Box3
double volume=0.0;//将盒子的体积存储在此处
//方框1规格
框1.设定长度(6.0);
框1.立根数(7.0);
框1.设置高度(5.0);
//方框2规格
框2.设定长度(12.0);
框2.立根数(13.0);
框2.设置高度(10.0);
//方框1的体积
volume=Box1.getVolume();

cout
nameofclassethree=one+two;
将使用编译器生成的复制构造函数从匿名临时
one+two
构造
three
one+two
是您实现的
one.operator+(two)
的语法糖

更智能的编译器可能会使用编译器生成的move构造函数,因为很明显,匿名临时文件不能被其他任何东西使用

您可以自己实现这些构造函数,也可以依赖生成复制(或移动)成员数据的编译器

一,

怎么做

NameOfClass three=one+two;
设置“三”的属性

NameOfClass three=1+2;
将被解释为
NameOfClass three=1。操作符+(2);
,而
three
将根据
NameOfClass::operator=()
的返回值进行复制/移动

二,

但是这个->长度应该是未定义的,而b.length是私有的。如何定义 它是混合在一起的吗

“这个->长度应该是未定义的”是什么意思?这里是
this
=
=
b
=
two

b.length
private
并不重要,因为
操作符+
是成员函数,它可以访问私有成员。

您对“一加二的+是作为参数传递的方法”的怀疑是正确的。
tmp.length = this->length + b.length;