C++:从类内访问运算符[]的便捷方法?
我有一个重载operator[]
(数组下标/括号运算符)的 C++ 类。 这在课堂之外非常方便,我可以在其中编写 foo[bar
]。 但是,当我在类中实现方法时,我不知道如何使用此表示法。
我知道我可以编写 operator[](bar)
或 this->operator[](bar)
但这些相当笨重,并且剥夺了很多便利首先是操作员。 (我也知道我可以添加一个调用运算符的新方法。)有没有办法可以编写 this[bar]
或 this->[bar]
或者类似的好东西?
I have a C++ class that overloads operator[]
, the array subscript/brackets operator. This is awfully convenient outside of my class, where I can write foo[bar
]. However, I can't figure out how to use this notation when I'm implementing methods inside my class.
I know I can write operator[](bar)
or this->operator[](bar)
but those are fairly unwieldy and take away a lot of the convenience of the operator in the first place. (I also know I can just add a new method that calls the operator.) Is there a way I can write this[bar]
or this->[bar]
or something similarly nice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对我来说效果很好。
works fine for me.
用于
调用实例对象的
operator[]
。假设
bar
是一个整数(或者可以自动转换为 1),this[bar]
将this
指针视为数组和索引该数组的第bar
元素。 除非this
在数组中,否则这将导致未定义的行为。 如果bar
不是整数,则可能会出现编译时错误。Use
to call the
operator[]
of the instance object.Assuming
bar
is an integer (or can be auto-converted to one),this[bar]
treats thethis
pointer as an array and indexes thebar
-th element of that array. Unlessthis
is in an array, this will result in undefined behavior. Ifbar
isn't integer-like, expect a compile-time error.我使用 at() 函数,并让operator[] 在幕后调用at() 函数,因此operator[] 只是语法糖。 这就是 std::vector 的做法,因此这似乎是一种合理(有优先级)的方法。
现在,对于完整的语法糖黑客(不能说我完全推荐它,但可能会引起您的兴趣):
此外,如果您想免费执行此操作,而不支付参考费用,和是一个致力于让程序员受苦的邪恶教派的追随者你可以这样做:
实际上我认为这就是Apple的NS API中大多数句柄的实现方式......
I use a at() function, and have the operator[] call the at() function behind the scenes, so operator[] is just syntactic sugar. That's how std::vector does it, so it seems like a reasonable (with precedence) way to do it.
Now for a complete syntactic sugar hack (can't say I fully recommend it but might strike your fancy):
Also if you want to do this for free, without paying the cost of the reference, AND are a follower of some evil sect dedicated to making programmers suffer you could do:
Actually I think that's how most handles are implemented in Apple's NS API...
(*this)[bar]
的替代方法是使用执行operator[]
工作的命名成员函数。 重载的运算符可以让用户的操作变得更轻松。 更重要的是,它们是班级界面的一部分。 问问自己,根据类自己的公共接口来实现类是否真的有意义。 如果没有,我建议编写一个单独的(受保护的或私有的)成员函数来完成这项工作,然后让operator[]
和任何其他函数调用它。An alternative to
(*this)[bar]
is to use a named member function that does the work ofoperator[]
. Overloaded operators make things easier on your users. More importantly, they are part of your class' interface. Ask yourself if it really makes sense to implement your class in terms of its own public interface. If not, I suggest writing a separate (protected or private) member function to do the work, and then haveoperator[]
and any other function call it.这应该也有效。 这个对我有用!
This should work too. It works for me!
您可以使用 (*this)[bar],但这可能并没有太大的改进......
You could use (*this)[bar], but that might not be much of an improvement...