C++:从类中访问 operator[] 的便捷方法? [英] C++: Convenient way to access operator[] from within class?

查看:73
本文介绍了C++:从类中访问 operator[] 的便捷方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C++ 类,它重载了数组下标/括号运算符 operator[].这在我的课外非常方便,我可以在那里编写 foo[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.

我知道我可以写 operator[](bar)this->operator[](bar) 但这些都相当笨拙并且带走了很多操作员的方便性放在首位.(我也知道我可以添加一个调用运算符的新方法.)有没有一种方法可以编写 this[bar]this->[bar]或者类似的东西?

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?

推荐答案

(*this)[bar];

对我来说效果很好.

这篇关于C++:从类中访问 operator[] 的便捷方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆