C++ 重载[]运算符并引用对象本身

C++ 重载[]运算符并引用对象本身,c++,operator-overloading,C++,Operator Overloading,我需要引用类主体内方法中的每个顶点。我尝试过使用this->,Solid::等,但效果也不太好 不管怎样,我已经把其他的东西都超载了,但我无法找到它,也无法在网上的任何地方搜索它 #define VERTICES_NR 8 class Solid { protected: Vector _vertices[VERTICES_NR]; // ... some other code (does not matter) ... // public: void Solid::Move()

我需要引用类主体内方法中的每个顶点。我尝试过使用
this->
Solid::
等,但效果也不太好

不管怎样,我已经把其他的东西都超载了,但我无法找到它,也无法在网上的任何地方搜索它

#define VERTICES_NR 8

class Solid {
protected:
  Vector _vertices[VERTICES_NR];

// ... some other code (does not matter) ... //


public:
  void Solid::Move()
  {
    Vector temp; // <- my own standalone type.

    cout << "How would you like to move the solid. Type like \"x y z\"" << endl;
    cin >> temp;

    for(int i = 0; i <= VERTICES_NR; i++)
      this->[i] = this->[i] + temp;
  }
}
#定义顶点\u NR 8
类实体{
受保护的:
向量_顶点[顶点_NR];
//…其他一些代码(不重要)//
公众:
void Solid::Move()
{
向量温度;//[i]+温度;
}
}

如何实现它?

重载运算符函数可以通过其名称显式调用,如下所示:

operator[](i) = operator[](i) + temp;

重载运算符函数可以通过其名称显式调用,如下所示:

operator[](i) = operator[](i) + temp;
你可以写得很简单

  for(int i = 0; i < VERTICES_NR; i++)
                  ^^^
    _vertices[i] += temp;
在这种情况下,您可以在类定义中使用

operator[]( i )

你可以写得很简单

  for(int i = 0; i < VERTICES_NR; i++)
                  ^^^
    _vertices[i] += temp;
在这种情况下,您可以在类定义中使用

operator[]( i )


直接呼叫接线员:

operator[](i) += temp;
或通过本:

(*this)[i] += temp;

直接呼叫接线员:

operator[](i) += temp;
或通过本:

(*this)[i] += temp;

错误:您正在访问类对象,而不是成员变量

更正

for(int i = 0; i <= VERTICES_NR; i++)
  vertices_[i] = vertices_[i] + temp;

错误:您正在访问类对象,而不是成员变量

更正

for(int i = 0; i <= VERTICES_NR; i++)
  vertices_[i] = vertices_[i] + temp;

你的
操作符[]
在哪里?你的
操作符[]
在哪里?在通用性的名义下,我想补充一点,这适用于在
this
上调用的所有操作符。在通用性的名义下,我想补充一点,这适用于在
this
上调用的所有操作符。是的,但我应该创建另一个重载(运算符+=)用于我的向量类,我不会这样做,因为我太懒了:D。或者我以后再做。是的,但我应该为我的向量类创建另一个重载(运算符+=),我不会这样做,因为我太懒了:D。或者我以后再做。