重载下标运算符并使用双指针?
我需要使用以下变量,并且必须围绕它编写自己的包装器以进行分配。我将超越赋值(因为我将不得不使用我制作的这个包装器)并希望重载包装器中的下标运算符,以便将其与双指针数组一起使用。我在代码中的意思是这样的:
我有什么:
从给定的库标头:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 C++ 来说,这是更典型的情况:
在这种情况下使用一个索引的一个大问题是,不清楚您正在尝试访问哪个索引(像素),同时将所有坐标计算推给客户端。如果它是单个指针,您仍然会将问题推到客户端,但您将能够以可预测的方式访问索引。
对于 double...内存中的布局可能会有所不同,它不一定是连续的。将其发布为单个值(逻辑上,作为一维数组)而不是二维数组或点(例如)是一个糟糕的设计。
this is more typical, for c++:
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).
目前尚不清楚为什么您首先使用双重间接。
如果
pixels
是一个指向像素数组的双指针,你可以这样做如果
pixels
是一个指向数组指针数组的指针,那么你需要两个索引:有这里发生了一些奇怪的事情。
typedef class { } 标识符
不是好的 C++。使用类标识符{ };
,否则类没有名称,因此不能在class { }
范围之外定义成员函数。 (还有其他问题。)int
可以完成同样的事情。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 doIf
pixels
is a pointer to an array of pointers to arrays, then you need two indices:There are a few weird things going on here.
typedef class { } identifier
is not good C++. Useclass identifier { };
, or else the class has no name, so you cannot define member functions outside theclass { }
scope. (Among other problems.)const int
. Plainint
accomplishes the same thing.使用 boost::multi_array
Use boost::multi_array