重载下标运算符[]

发布于 2024-10-20 07:34:46 字数 458 浏览 0 评论 0原文

为什么它的操作需要是类的成员函数,并且最好返回对私有成员的引用?

class X
{
public:

    int& operator[] (const size_t);
    const int &operator[] (const size_t) const;

private:
    static std::vector<int> data;
};

int v[] = {0, 1, 2, 3, 4, 5};
std::vector<int> X::data(v, v+6);

int& X::operator[] (const size_t index)
{
    return data[index];
}

const int& X::operator[] (const size_t index) const
{
    return data[index];
}

Why does it require to be a member function of a class for its operation and is good to return a reference to private member?

class X
{
public:

    int& operator[] (const size_t);
    const int &operator[] (const size_t) const;

private:
    static std::vector<int> data;
};

int v[] = {0, 1, 2, 3, 4, 5};
std::vector<int> X::data(v, v+6);

int& X::operator[] (const size_t index)
{
    return data[index];
}

const int& X::operator[] (const size_t index) const
{
    return data[index];
}

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

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

发布评论

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

评论(4

黄昏下泛黄的笔记 2024-10-27 07:34:46
  • 至于为什么需要[]作为成员,您可以阅读这个问题(由您真诚地提出)。好像就是这样,没有真正令人信服的解释。

  • 至于为什么要返回引用呢?
    因为您不仅希望提供一种读取数据的方法,还希望提供一种修改数据的方法(对于非常量对象)。如果返回的不是引用(或某个代理)

    v[i] = 4;

    行不通。

华泰

  • As to why is it required to have [] as a member, you can read this question (by yours sincerely). Seems it's just the way it is with no really really convincing explanation.

  • As to why return reference?
    Because you want to provide a way not only to read, but also (for non-const objects) to modify the data. If the return weren't a reference (or some proxyr)

    v[i] = 4;

    wouldn't work.

HTH

¢蛋碎的人ぎ生 2024-10-27 07:34:46

根据 13.5.5,它必须是成员函数:

operator[] 应该是非静态的
成员函数恰好为一
范围。它实现了
下标语法

对私有成员的引用是完全可以的并且非常常见。您向类的用户隐藏了详细信息,但仍然提供您需要的功能(修改单个元素的能力)

您的 data 变量可能不应该是静态的,除非您真的想共享它在你班级的所有实例中

It needs to be a member function according to 13.5.5:

operator[] shall be a non-static
member function with exactly one
parameter. It implements the
subscripting syntax

A reference to a private member is completely OK and pretty common. You hide the details from the user of your class, but still provide the functionality you need (ability to modify individual elements)

Your data variable likely shoudn't be static though, unless you really want to share it among all instances of your class

紅太極 2024-10-27 07:34:46

对于第一个问题,这只是他们决定的方式,即你不能这样做:

T operator[]( const X &, size_t );

作为外部函数。

是的,您可以返回对私有成员的引用,如果您允许用户在那里写入,则返回非常量,否则返回非常量。

在您的示例中,虽然数据是静态的,但如果这是它返回的数据源,则没有意义。

For the first question, it is just the way they decided it had to be, i.e. you can't do:

T operator[]( const X &, size_t );

as an external function.

And yes, you are fine returning a reference to a private member, non-const if you allow your users to write there, non-const otherwise.

In your example though data is static, which does not make sense if that is the source for what it returns.

东京女 2024-10-27 07:34:46

调用非成员运算符[] 的语法是什么?任何语法都会很尴尬。 operator[] 采用 [] 中的一个参数,该参数通常是查找对象所需的索引或某种数据。

另外,是的,返回引用是个好主意,即使它是私人成员。这正是 STL 向量所做的事情,以及我能想到的任何其他提供 operator[] 的类的作用。建议保持使用。

What would the syntax be for calling a non-member operator[]? Any syntax for that would be awkward. operator[] takes ones parameter within the [ and ] and that is usually an index or some kind of data necessary to find an object.

Also, yes, it is a good idea to return a reference, even if it's a private member. That is exactly what STL vectors do and just about any other class I can think of that I've ever used that provides operator[]. It would be advised that it the usage is maintained.

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