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++
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) = ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With