Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 重载下标运算符并使用双指针?_C++_Double Pointer_Subscript Operator - Fatal编程技术网

C++ 重载下标运算符并使用双指针?

C++ 重载下标运算符并使用双指针?,c++,double-pointer,subscript-operator,C++,Double Pointer,Subscript Operator,我需要处理以下变量,并且必须为赋值编写自己的包装器。我已经超出了赋值范围(因为我将不得不使用我制作的这个包装器),并且希望在包装器中重载下标操作符,以便将其用于双指针数组。我在代码中的意思是: 我所拥有的: 从库的给定标题: typedef struct { // A pixel stores 3 bytes of data: byte red; // intensity of the red component byte green; //

我需要处理以下变量,并且必须为赋值编写自己的包装器。我已经超出了赋值范围(因为我将不得不使用我制作的这个包装器),并且希望在包装器中重载下标操作符,以便将其用于双指针数组。我在代码中的意思是:

我所拥有的:

从库的给定标题:

typedef struct {        // A pixel stores 3 bytes of data:
    byte red;       //  intensity of the red component
    byte green;     //  intensity of the green component
    byte blue;      //  intensity of the blue component
} pixel;

typedef struct { 
    int   rows, cols;   /* pic size */
    pixel **pixels;     /* image data */
} image;
我的班级(当然包括在页眉中):


当然,这是行不通的,因为双指针返回一个指针,这不是我要它返回的,但是返回*pixels&也不会返回它。为了满足我的好奇心并帮助我理解为什么这是不可能的,有人能告诉我,如果可以的话,这将如何实现,以及为什么是这样吗?请记住,我还不太了解指针(我知道指针的基本工作原理,但仅此而已),我希望用它来扩大我的理解。

首先不清楚为什么要使用双间接寻址

如果
pixels
是指向像素数组的双指针,则可以执行以下操作

pixels& MyWrapper::operator[] (const int nIndex) {
    return (*Image.pixels)[nIndex]; // where Image is of type image
}
如果
pixels
是指向数组指针数组的指针,则需要两个索引:

pixels& MyWrapper::operator() ( int xIndex, int yIndex ) {
    return Image.pixels[yIndex][xIndex]; // where Image is of type image
}

这里发生了一些奇怪的事情

    代码> TyWupe类{}标识符< /C> >不好C++。使用
    类标识符{}
    ,否则该类没有名称,因此不能在
    类{}
    范围之外定义成员函数。(除其他问题外)
  • 没有理由创建参数类型
    const int
    。普通
    int
    完成同样的事情
  • 这种双重间接性没有明显的原因。通常在C++中,我们避免直接使用指针。您可能可以使用预先打包的标准结构

首先不清楚为什么要使用双间接寻址

如果
pixels
是指向像素数组的双指针,则可以执行以下操作

pixels& MyWrapper::operator[] (const int nIndex) {
    return (*Image.pixels)[nIndex]; // where Image is of type image
}
如果
pixels
是指向数组指针数组的指针,则需要两个索引:

pixels& MyWrapper::operator() ( int xIndex, int yIndex ) {
    return Image.pixels[yIndex][xIndex]; // where Image is of type image
}

这里发生了一些奇怪的事情

    代码> TyWupe类{}标识符< /C> >不好C++。使用
    类标识符{}
    ,否则该类没有名称,因此不能在
    类{}
    范围之外定义成员函数。(除其他问题外)
  • 没有理由创建参数类型
    const int
    。普通
    int
    完成同样的事情
  • 这种双重间接性没有明显的原因。通常在C++中,我们避免直接使用指针。您可能可以使用预先打包的标准结构

    • 使用

      使用

      这对于c++来说更为典型:

      #include <vector>
      
      namespace AA {
      
          class t_point {
          public:
              t_point(const size_t& X, const size_t& Y) : d_x(X), d_y(Y) {
              }
             const size_t& x() const { return this->d_x; }
              const size_t& y() const { return this->d_y; }
      
          private:   
              size_t d_x;
              size_t d_y;
          };
      
          class t_array {
          public:
              // abusive, but possible. prefer `at`
              const int& operator[](const t_point& idx) const {
                  return this->at(idx.x(), idx.y());
              }
      
              const int& at(const t_point& idx) const {
                  return this->at(idx.x(), idx.y());
              }
      
              const int& at(const size_t& x, const size_t& y) const {
                  return this->d_objects[x][y];
              }
          private:
              // or use your c image representation...
              std::vector<std::vector<int> > d_objects;
          private:
              static const int& ctest(const t_array& arr) {
                  const t_point pt(1, 2);
                  return arr[pt];
                  return arr.at(pt);
                  return arr.at(pt.d_x, pt.d_y);
              }
          };
      
      }
      
      #包括
      名称空间AA{
      类t_点{
      公众:
      t_点(常数大小t&X,常数大小t&Y):d_X(X),d_Y(Y){
      }
      const size_t&x()const{返回此->d_x;}
      const size\u t&y()const{返回此->d_y;}
      私人:
      尺寸d_x;
      大小;
      };
      类t_数组{
      公众:
      //辱骂,但可能。更喜欢“at”`
      常量int和运算符[](常量t_点和idx)常量{
      返回此->at(idx.x(),idx.y());
      }
      常量内部和at(常量t_点和idx)常量{
      返回此->at(idx.x(),idx.y());
      }
      常数内和外(常数大小t&x,常数大小t&y)常数{
      返回此->d_对象[x][y];
      }
      私人:
      //或者使用您的c图像表示。。。
      std::向量d_对象;
      私人:
      静态常量int和ctest(常量t_数组和arr){
      常数t_点pt(1,2);
      返回arr[pt];
      返回终点(pt);
      返回终点(点d_x,点d_y);
      }
      };
      }
      
      在这种情况下,使用一个索引的最大问题是,在将所有坐标计算推送到客户机时,不清楚要访问哪个索引(像素)。如果是单个指针,您仍然会将问题推送到客户端,但您可以预测地访问索引


      用双。。。内存中的布局可能会有所不同,但不一定是连续的。将其发布为单个值(逻辑上是一维数组),而不是二维数组或点(例如),这只是一个糟糕的设计。

      对于c++来说,这更为典型:

      #include <vector>
      
      namespace AA {
      
          class t_point {
          public:
              t_point(const size_t& X, const size_t& Y) : d_x(X), d_y(Y) {
              }
             const size_t& x() const { return this->d_x; }
              const size_t& y() const { return this->d_y; }
      
          private:   
              size_t d_x;
              size_t d_y;
          };
      
          class t_array {
          public:
              // abusive, but possible. prefer `at`
              const int& operator[](const t_point& idx) const {
                  return this->at(idx.x(), idx.y());
              }
      
              const int& at(const t_point& idx) const {
                  return this->at(idx.x(), idx.y());
              }
      
              const int& at(const size_t& x, const size_t& y) const {
                  return this->d_objects[x][y];
              }
          private:
              // or use your c image representation...
              std::vector<std::vector<int> > d_objects;
          private:
              static const int& ctest(const t_array& arr) {
                  const t_point pt(1, 2);
                  return arr[pt];
                  return arr.at(pt);
                  return arr.at(pt.d_x, pt.d_y);
              }
          };
      
      }
      
      #包括
      名称空间AA{
      类t_点{
      公众:
      t_点(常数大小t&X,常数大小t&Y):d_X(X),d_Y(Y){
      }
      const size_t&x()const{返回此->d_x;}
      const size\u t&y()const{返回此->d_y;}
      私人:
      尺寸d_x;
      大小;
      };
      类t_数组{
      公众:
      //辱骂,但可能。更喜欢“at”`
      常量int和运算符[](常量t_点和idx)常量{
      返回此->at(idx.x(),idx.y());
      }
      常量内部和at(常量t_点和idx)常量{
      返回此->at(idx.x(),idx.y());
      }
      常数内和外(常数大小t&x,常数大小t&y)常数{
      返回此->d_对象[x][y];
      }
      私人:
      //或者使用您的c图像表示。。。
      std::向量d_对象;
      私人:
      静态常量int和ctest(常量t_数组和arr){
      常数t_点pt(1,2);
      返回arr[pt];
      返回终点(pt);
      返回终点(点d_x,点d_y);
      }
      };
      }
      
      在这种情况下,使用一个索引的最大问题是,在将所有坐标计算推送到客户机时,不清楚要访问哪个索引(像素)。如果是单个指针,您仍然会将问题推送到客户端,但您可以预测地访问索引

      用双。。。内存中的布局可能会有所不同,但不一定是连续的。这只是一个糟糕的设计