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

发布于 2024-10-31 06:56:47 字数 850 浏览 3 评论 0原文

我需要使用以下变量,并且必须围绕它编写自己的包装器以进行分配。我将超越赋值(因为我将不得不使用我制作的这个包装器)并希望重载包装器中的下标运算符,以便将其与双指针数组一起使用。我在代码中的意思是这样的:

我有什么:

从给定的库标头:

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& MyWrapper::operator[] (const int nIndex) {
    return Image.pixels[nIndex]; // where Image is of type image
}

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

I have the following variable that I need to work with, and have to write my own wrapper around it for an assignment. I am going beyond the assignment (since I am going to have to use this wrapper I make) and wanting to overload the subscript operator in my wrapper in order to use it with the double pointer array. What I mean in code is this:

What I have:

From given header for library:

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;

My class (of course included in header):

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

Of course this won't work since the double pointer returns a pointer, which is not what I'm telling it to return, yet returning *pixels& doesn't return it either. Just to sate my curiosity and to help me understand why this isn't possible, could someone tell me how this would be implemented if it can be at all, and why it is that way? Keep in mind I don't understand pointers very well yet (I know of the basics of how they work, but thats it), and am hoping to use this to very much broaden my understanding.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天赋异禀 2024-11-07 06:56:47

对于 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);
        }
    };

}

在这种情况下使用一个索引的一个大问题是,不清楚您正在尝试访问哪个索引(像素),同时将所有坐标计算推给客户端。如果它是单个指针,您仍然会将问题推到客户端,但您将能够以可预测的方式访问索引。

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

this is more typical, for 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);
        }
    };

}

the big problem with using one index in this case it that it is not clear which index (pixel) you are attempting to access, while pushing all the coordinate calculations off to the client. if it's a single pointer, you'd still push the problem onto the client, but you'd be able access the index predictably.

with double... the layout in memory can vary, it is not necessarily contiguous. it's just a bad design to publish it as a single value (logically, as a 1D array), rather than a 2D array or a point (for example).

送你一个梦 2024-11-07 06:56:47

目前尚不清楚为什么您首先使用双重间接。

如果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
}

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

  • typedef class { } 标识符 不是好的 C++。使用类标识符{ };,否则类没有名称,因此不能在class { }范围之外定义成员函数。 (还有其他问题。)
  • 没有理由将参数类型设置为 const int。普通的 int 可以完成同样的事情。
  • 双重间接没有明显的原因。通常在 C++ 中我们避免直接使用指针。您可能可以使用一个预先打包的标准结构。

It's not clear why you are using double indirection in the first place.

If pixels is a double pointer to an array of pixels, you can do

pixels& MyWrapper::operator[] (const int nIndex) {
    return (*Image.pixels)[nIndex]; // where Image is of type image
}

If pixels is a pointer to an array of pointers to arrays, then you need two indices:

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

There are a few weird things going on here.

  • typedef class { } identifier is not good C++. Use class identifier { };, or else the class has no name, so you cannot define member functions outside the class { } scope. (Among other problems.)
  • There is no reason to make a parameter type const int. Plain int accomplishes the same thing.
  • There is no apparent reason for the double indirection. Typically in C++ we avoid the direct use of pointers. There is probably a prepackaged standard structure you can use instead.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文