如何访问 C++ 来自它所在的类中的下标运算符?

发布于 2024-07-16 01:16:51 字数 621 浏览 6 评论 0原文

其中,ClassA 有一个这样的运算符,它返回 ClassB:

class ClassA
{
public:
    ClassA();
    ClassB &operator[](int index);
}

如果我想从 ClassA 的构造函数中访问所述运算符,如下所示:

ClassA::ClassA()
{
    // How do I access the [] operator?
}

目前,作为一种解决方法,我只是使用名为 GetAtIndex(int index) 的方法[] 运算符调用它,构造函数也调用它。

如果我能像 C# 一样访问它那就太好了:

// Note: This is C#
class ClassA
{
   ClassB this[int index]
   {
       get { /* ... */ }
       set { /* ... */ }
   }

   void ClassA()
   {
       this[0] = new ClassB();
   }
}

注意:我正在使用 g++

Where, ClassA has an operator as such, that returns ClassB:

class ClassA
{
public:
    ClassA();
    ClassB &operator[](int index);
}

If I want to access said operator from within ClassA's constructor, as so:

ClassA::ClassA()
{
    // How do I access the [] operator?
}

At the moment, as a work-around I'm just using a method called GetAtIndex(int index) which the [] operator calls, and so does the constructor.

It would be nice if I could access it in the same as as C# works:

// Note: This is C#
class ClassA
{
   ClassB this[int index]
   {
       get { /* ... */ }
       set { /* ... */ }
   }

   void ClassA()
   {
       this[0] = new ClassB();
   }
}

Note: I'm using g++

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

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

发布评论

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

评论(5

破晓 2024-07-23 01:16:52
 ClassA::ClassA()
 {
     this->operator[]( someindex ) = whatever;
 }

但请确保在使用操作符之前,该操作符可能依赖的任何成员数据都已完全构造完毕。

 ClassA::ClassA()
 {
     this->operator[]( someindex ) = whatever;
 }

But make sure that whatever member data the operator may depend on is fully constructed before you use it.

谈场末日恋爱 2024-07-23 01:16:52

两种不同的方式:

( *this ) [ index ] or this -> operator [ ] ( index )

Two different ways:

( *this ) [ index ] or this -> operator [ ] ( index )
夜无邪 2024-07-23 01:16:52

还有一个方法,不知道有没有用...

ClassA::ClassA()
{
    ClassA &_this = *this;
    _this[0] = someValue;
    _this[1] = someValue;
    _this[2] = someValue;
    _this[3] = someValue;
    _this[4] = someValue;
}

Here's another way, not sure if it's useful...

ClassA::ClassA()
{
    ClassA &_this = *this;
    _this[0] = someValue;
    _this[1] = someValue;
    _this[2] = someValue;
    _this[3] = someValue;
    _this[4] = someValue;
}
小耗子 2024-07-23 01:16:51

你可以使用:

this->operator[](0) = ...

或:

(*this)[0] = ...

但语法有点尴尬。 我通常创建另一个名为 get 的方法并使用它,例如:

ClassB& get(size_t index) {
    return this->operator[](0); // or something more direct
}

然后当你想使用它时,你可以说:

this->get(0) = ...

You can use:

this->operator[](0) = ...

or:

(*this)[0] = ...

But the syntax is a little awkward. I usually make another method called get and use that, e.g.:

ClassB& get(size_t index) {
    return this->operator[](0); // or something more direct
}

Then when you want to use it you can just say:

this->get(0) = ...
宫墨修音 2024-07-23 01:16:51

尝试以下操作

(*this)[0] = ...

Try the following

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