返回对底层数组段的引用

huangapple go评论151阅读模式
英文:

Returning reference to segments of underlying array

问题

我正在努力通过编写一个类来提高C++的水平,类似于向量的对象。

我希望它具有一个重载运算符[],接受一个std::initializer_list<int>,用于输入底层数组的索引,并返回数组中这些元素的引用。

所以像这样的东西是可能的:

MyVector arr = {1, 2, 3, 4};

arr[{0, 2}] = 3;
// 或者
arr[{0, 2}] = {5, 6};

我考虑过创建另一个类,该类继承自MyVector,并为这个类编写一个赋值运算符

MyVector::operator[](std::initializer_list<int> ind){
    return MyVectorRef(this, ind);
}

class MyVectorRef: private MyVector{
   MyVector* base;
   std::initializer_list<int> indices;

   MyVectorRef(MyVector* base, ind): base(base), indices(ind) {};

   MyVector operator=(std::initializer_list<int> cp){
      for(int i: indices){
          (*base).data[i] = cp[i];
      }
   }
   // 对于第一种情况也是类似的...
};

我知道这很巧妙,可能甚至不起作用。而且,如果我希望进行方法级联,可能会出现一些问题。

是否有更好的方法来解决这个问题?

英文:

Im trying to get better at C++ by coding up a class for a vector like object.

I wish for it to have an operator overloading for [], that takes in an std::initializer_list<int> for input of indices on underlying array, and returns a reference to those elements of the array.

So something like this would be possible:

MyVector arr = {1, 2, 3, 4};

arr[{0, 2}] = 3;
// or 
arr[{0, 2}] = {5, 6};

What I've thought of is to create another class that inherits MyVector, and write an assignment operator for this class

MyVector::operator[](std::initializer_list<int> ind){
    return MyVectorRef(this, ind);
}

class MyVectorRef: private MyVector{
   MyVector* base;
   std::initializer_list<int> indices;

   MyVectorRef(MyVector* base, ind): base(base), indices(ind) {};

   MyVector operator=(std::initializer_list<int> cp){
      for(int i: indices){
          (*base).data[i] = cp[i];
      }
   }
   // similarly for the first case...
};

I know this is very hacky, maybe wont even work. Also, may be of some problem if i wish to have method cascading.

Is there a better way to work around this?

答案1

得分: 1

几个问题:你的 operator[] 函数签名不正确:它应该是 MyVectorRef MyVector::operator[](std::initializer_list<int> ind),因为C++不允许省略operator[]的返回类型。

如果你在 operator[] 下声明了 MyVectorRef,当C++编译 MyVectorRef operator[](...) 这一行时,C++不会识别 MyVectorRef 是什么,所以会抛出错误。要解决这个问题,你需要在 MyVector 上方添加一个不完整的类声明,声明 MyVectorRef,就像这样:

class MyVectorRef; // MyVectorRef 的不完整类声明

class MyVector {
   //...

   MyVectorRef operator[](std::initializer_list<int> ind); // operator[] 的方法声明
};

class MyVectorRef {
   MyVector* base;
   std::initializer_list<int> indices;
   //...

}; // MyVectorRef 的完整定义

MyVectorRef MyVector::operator[](std::initializer_list<int> ind) {
   return MyVectorRef(this, ind);
}

不完整类声明的目的是告诉编译器 MyVectorRef 标记存在,这样当编译器编译 operator[] 的方法声明时,它知道 MyVectorRef 是一个类。

英文:

A few issues: Your function signature for operator[] is incorrect: it should be MyVectorRef MyVector::operator[](std::initializer_list&lt;int&gt; ind), as C++ doesn't allow you to omit the return type for operator[].

If you declare MyVectorRef under operator[], when C++ compiles the line MyVectorRef operator[](...), C++ doesn't recognize what MyVectorRef is, so it throws an error. To solve this, you have to add an incomplete class declaration for MyVectorRef above MyVector.

So something like this:

class MyVectorRef; //incomplete class declaration for MyVectorRef

class MyVector {
   //...

   MyVectorRef operator[](std::initializer_list&lt;int&gt; ind); //method declaration for operator[]
};

class MyVectorRef {
   MyVector* base;
   std::initializer_list&lt;int&gt; indices;
   //...

}; //complete definition of MyVectorRef

MyVectorRef MyVector::operator[](std::initializer_list&lt;int&gt; ind) {
   return MyVectorRef(this, ind);
}

The reason for the incomplete class declaration is essentially to tell the compiler that the token MyVectorRef exists, so that when the compiler compiles the method declaration for operator[], it knows that MyVectorRef is a class.

huangapple
  • 本文由 发表于 2023年8月11日 00:32:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定