C++ 什么是C+中的重载运算符+;?

C++ 什么是C+中的重载运算符+;?,c++,operator-overloading,C++,Operator Overloading,我意识到这是一个基本的问题,但我在网上搜索过,访问过cplusplus.com,阅读过我的书,似乎无法理解重载运算符的概念。cplusplus.com中的一个具体示例是: // vectors: overloading operators example #include <iostream> using namespace std; class CVector { public: int x,y; CVector () {}; CVector (int

我意识到这是一个基本的问题,但我在网上搜索过,访问过cplusplus.com,阅读过我的书,似乎无法理解重载运算符的概念。cplusplus.com中的一个具体示例是:

// vectors: overloading operators example
#include <iostream>
using namespace std;

class CVector {
  public:
    int x,y;
    CVector () {};
    CVector (int,int);
    CVector operator + (CVector);
};

CVector::CVector (int a, int b) {
  x = a;
  y = b;
}

CVector CVector::operator+ (CVector param) {
  CVector temp;
  temp.x = x + param.x;
  temp.y = y + param.y;
  return (temp);
}

int main () {
  CVector a (3,1);
  CVector b (1,2);
  CVector c;
  c = a + b;
  cout << c.x << "," << c.y;
  return 0;
}
从“”

我正在处理的当前作业需要重载一个++和一个--运算符

提前感谢您提供的信息,对于这个有点模糊的问题,我感到抱歉,不幸的是,我对它一点也不确定。

本例中的“运算符”是+符号

这里的想法是操作员做一些事情。重载运算符执行不同的操作

因此,在本例中,通常用于添加两个数字的“+”运算符被“重载”以允许添加向量或时间

<>编辑:在C++中内置两个整数;当您这样做时,编译器会自动理解您的意思

int x, y = 2, z = 2; 
x = y + z;
另一方面,对象可以是任何东西,因此在两个对象之间使用“+”在本质上没有任何意义。如果你有

Apple apple1, apple2, apple3;
apple3 = apple1 + apple2;

将两个Apple对象添加在一起意味着什么?没有什么,直到你超载了‘+’运算符,并告诉编译器当你把两个Apple对象加在一起时,你的意思是什么。< /P> < P> C++中的一个运算符只是一个具有特殊名称的函数。因此,不要说
Add(int,int)
而是说
operator+(int,int)

现在,和任何其他函数一样,您可以重载它来表示在其他类型上工作。在向量示例中,如果您重载
运算符+
以获取
CVector
参数(即
运算符+(CVector,CVector)
),则可以说:

CVector a,b,res;
res=a+b;
由于
++
--
是一元的(它们只接受一个参数),要重载它们,您需要:

type operator ++(type p)
{
  type res;
  res.value++;

  return res;
}
其中,
type
是具有名为
value
的字段的任何类型。你明白了。操作符应该是“+”、“-”或“+=”。这些对现有对象执行不同的方法。这实际上归结为一个方法调用。除了正常的方法调用之外,这些调用对于人类用户来说更为自然。写“1+2”看起来更正常,比“add(1,2)”短。如果重载运算符,则会更改它执行的方法

在第一个示例中,“+”运算符的方法被重载,因此可以将其用于向量加法


我建议您将第一个示例复制到编辑器中,并对其进行一些处理。一旦你理解了代码的执行,我的建议是实现向量减法和乘法。

< P>运算符重载是C++提供的技术,让你定义语言中的运算符如何应用于非内置对象。p> 在
+
运算符的
时间
类运算符重载示例中:

Time operator+(const Time& lhs, const Time& rhs);
使用该重载,您现在可以以“自然”方式对
Time
对象执行添加操作:

Time t1 = some_time_initializer;
Time t2 = some_other_time_initializer;

Time t3 = t1 + t2;    // calls operator+( t1, t2)
运算符的重载只是一个函数,其特殊名称“operator”后跟重载运算符的符号。大多数运算符都可以重载-不能重载的运算符包括:

.  .*  :: and ?:
您可以直接按名称调用函数,但通常不能(运算符重载的目的是能够正常使用运算符)

被调用的重载函数由运算符的参数的正常重载解析决定-这就是编译器如何知道调用使用上述示例中的
时间
参数类型的
运算符+()

重载
++
-->
递增和递减运算符时,需要注意的另一件事是,每种运算符都有两种版本-前缀和后缀形式。这些运算符的后缀版本使用一个额外的
int
参数(该参数传递为0,除了区分这两种类型的运算符之外,没有其他用途)。C++标准有以下例子:

class X {
public:
    X&   operator++();      //prefix ++a
    X    operator++(int);   //postfix a++
};

class Y { };

Y&   operator++(Y&);        //prefix ++b
Y    operator++(Y&, int);   //postfix b++
您还应该知道,重载运算符不必执行与内置运算符类似的操作,因为它们或多或少都是正常函数,它们可以执行任何您想要的操作。例如,标准库的IO流接口对流的输出和输入使用移位运算符,这与位移位完全不同。然而,如果您试图对运算符重载过于花哨,您将给那些试图遵循您的代码的人(甚至可能是您在稍后查看代码时)带来很多困惑


使用运算符重载小心。

重载运算符是当使用运算符处理C++没有“本地”支持该运算符的类型时使用的。

例如,通常可以使用二进制“+”运算符添加数值(浮点、整数、双精度等)。还可以向指针添加整数类型-例如:

char foo[] = "A few words";
char *p = &(foo[3]);     // Points to "e"
char *q = foo + 3;       // Also points to "e"
std::string a("A short"), b(" string.");
std::string c = a + b;  // c is "A short string."
但就是这样!您不能再使用二进制“+”运算符以本机方式执行任何操作

<>但是,运算符重载允许您做C++中的设计器没有构建到语言中的事情,比如使用+运算符来连接字符串——例如:

char foo[] = "A few words";
char *p = &(foo[3]);     // Points to "e"
char *q = foo + 3;       // Also points to "e"
std::string a("A short"), b(" string.");
std::string c = a + b;  // c is "A short string."

一旦你开始思考这个问题,维基百科的例子就会变得更有意义。

在开始之前,有很多运营商!下面是所有C++操作符的列表:.< /P>

这样说,C++中的运算符重载是使某个操作符以特定的方式对对象进行操作的一种方法。 例如,如果在对象上使用递增/递减运算符(++和--),编译器将无法理解对象中需要递增/递减的内容,因为它不是基元类型(int、char、float…)。您必须为编译器定义适当的行为才能理解您的意思。运算符重载基本上告诉编译器在对象中使用递增/递减运算符时必须完成的操作

还有,游牧
CVector& CVector::operator=(const CVector& rhs)
{
    delete[] data;
    capacity = length = rhs.length;
    data = new double[length];
    memcpy(data, rhs.data, length * sizeof(double));
    return (*this);
}
class Vector2
{
  double m_x, m_y;
public:
  Vector2(double x, double y) : m_x(x), m_y(y) {}
  // Vector2(const Vector2& other) = default;
  // Vector2& operator=(const Vector2& other) = default;

  Vector2& operator+=(const Vector2& addend) { m_x += addend.m_x; m_y += addend.m_y; return *this; }
  Vector2 operator+(const Vector2& addend) const { Vector2 sum(*this); return sum += addend; }
};
std::complex<double> a(1,2), b(3,4), c( 5, 6 );
std::complex<double> d = a + b + c;         // compare to d = a.add(b).add(c);
std::complex<double> e = (a + d) + (b + c); // e = a.add(d).add( b.add(c) );
struct type {
   type( int x ) : value(x) {}
   int value;
};
bool operator==( type const & lhs, type const & rhs ) 
   { return lhs.value == rhs.value; }
bool operator!=( type const & lhs, type const & rhs ) 
   { return !lhs == rhs; }

std::vector<type> getObjects(); // creates and fills a vector
int main() {
   std::vector<type> objects = getObjects();
   type t( 5 );
   std::find( objects.begin(), objects.end(), t );
}
bool operator<( type const & lhs, type const & rhs )
{
   return lhs < rhs;
}
std::map<type, int> m; // m will use the natural `operator<` order
MyVector a, b;
MyVector c = a + b;
MyVector c = append( a, b );
class vector {
public:
   int operator[]( int );
};
vector v;
std::cout << v[0] << std::endl;

class matrix {
public:
   int operator()( int row, int column ); 
      // operator[] cannot be overloaded with more than 1 argument
};
matrix m;
std::cout << m( 3,4 ) << std::endl;