C++:从类内访问运算符[]的便捷方法?

发布于 2024-07-14 08:45:12 字数 341 浏览 6 评论 0原文

我有一个重载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 技术交流群。

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

发布评论

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

评论(6

谎言月老 2024-07-21 08:45:12
(*this)[bar];

对我来说效果很好。

(*this)[bar];

works fine for me.

不爱素颜 2024-07-21 08:45:12

用于

(*this)[bar]

调用实例对象的operator[]

假设 bar 是一个整数(或者可以自动转换为 1),this[bar]this 指针视为数组和索引该数组的第 bar 元素。 除非 this 在数组中,否则这将导致未定义的行为。 如果 bar 不是整数,则可能会出现编译时错误。

Use

(*this)[bar]

to call the operator[] of the instance object.

Assuming bar is an integer (or can be auto-converted to one), this[bar] treats the this pointer as an array and indexes the bar-th element of that array. Unless this is in an array, this will result in undefined behavior. If bar isn't integer-like, expect a compile-time error.

可爱咩 2024-07-21 08:45:12

我使用 at() 函数,并让operator[] 在幕后调用at() 函数,因此operator[] 只是语法糖。 这就是 std::vector 的做法,因此这似乎是一种合理(有优先级)的方法。

现在,对于完整的语法糖黑客(不能说我完全推荐它,但可能会引起您的兴趣):

class Widget
{
    Widget&     self;
public:
    Widget() :self(*this)
    {}

    void operator[](int)
    {
        printf("hello");
    }

    void test()
    {
        //scripting like sugar
        //you pay the price of an extra reference per class though
        self[1]; 
    }
};


int main(int argc, char* argv[])
{
    Widget w;
    w[1];
    w.test();
    return 0;
}

此外,如果您想免费执行此操作,而不支付参考费用,是一个致力于让程序员受苦的邪恶教派的追随者你可以这样做:

#define self (*this)

实际上我认为这就是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):

class Widget
{
    Widget&     self;
public:
    Widget() :self(*this)
    {}

    void operator[](int)
    {
        printf("hello");
    }

    void test()
    {
        //scripting like sugar
        //you pay the price of an extra reference per class though
        self[1]; 
    }
};


int main(int argc, char* argv[])
{
    Widget w;
    w[1];
    w.test();
    return 0;
}

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:

#define self (*this)

Actually I think that's how most handles are implemented in Apple's NS API...

辞慾 2024-07-21 08:45:12

(*this)[bar] 的替代方法是使用执行 operator[] 工作的命名成员函数。 重载的运算符可以让用户的操作变得更轻松。 更重要的是,它们是班级界面的一部分。 问问自己,根据类自己的公共接口来实现类是否真的有意义。 如果没有,我建议编写一个单独的(受保护的或私有的)成员函数来完成这项工作,然后让 operator[] 和任何其他函数调用它。

An alternative to (*this)[bar] is to use a named member function that does the work of operator[]. 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 have operator[] and any other function call it.

无远思近则忧 2024-07-21 08:45:12
 operator[](bar) 

这应该也有效。 这个对我有用!

 operator[](bar) 

This should work too. It works for me!

天生の放荡 2024-07-21 08:45:12

您可以使用 (*this)[bar],但这可能并没有太大的改进......

You could use (*this)[bar], but that might not be much of an improvement...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文