6

I have a class which inherits from another class, and I wish to call [index] to access the index'th element of some allocated storage.

Here is a minimal example:

class A
{
    protected:
    double *mem;

    double operator[](const size_t index)
    {
        return mem[index];
    }
}

class B : public A
{
    void function()
    {
        double var = this->operator[](0);
    }
}

So here I step around the problem by calling this->operator[](0) which is kind of messy.

Is this the correct way to access elements of mem considering that I don't have access to that variable from the derived class, or is there an alternative way?

Edit: I think it might be significant that I'm conforming to C++11, so can't call mem[0]?

Edit, template classes

As discussed below, the compiler error I see isn't showing up for this example, because there are no templates here.

To reproduce the compiler error:

template <typename T>
class A
{
    protected:
    double *mem;

    double operator[](const size_t index)
    {
        return mem[index];
    }
}

template <typename T>
class B : public A<T>
{
    void function()
    {
        double var = this->operator[](0);
    }
}

Possible Solutions

return this->operator[](0);
return (*this)[0];
return (this->mem)[0];
return *((this->mem)+0);
return (*this).mem[0];
return *((*this).mem+0);

... I think all of these do what I expect them to. Any more suggestions?

Even better solution:

return A::mem[0];
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • You can also do `(*this)[0];` or `mem[0]` directly. – James Adkison Mar 05 '16 at 17:49
  • 2
    "Is this the correct way to access elements of mem considering that I don't have access to that variable from the derived class" -- You do have access from the derived class because `mem` is declared `protected`. – James Adkison Mar 05 '16 at 17:53
  • Possible duplicate of [How to use base class's constructors and assignment operator in C++?](http://stackoverflow.com/questions/1226634/how-to-use-base-classs-constructors-and-assignment-operator-in-c) – mustafagonul Mar 05 '16 at 18:09
  • 1
    "I'm conforming to C++11, so can't call mem[0]" a non sequitur. – n. m. could be an AI May 03 '23 at 09:57
  • @n.m. If the previous (2) edits are wrong please edit and change again – FreelanceConsultant May 03 '23 at 13:29
  • This is an open-ended question that is calling for a vague and possibly infinite set of suggestions, none of which are necessarily better than others. Please limit the scope of the question such that it can be answered with a single authoritative answer. For example, "Why doesn't `mem[0]` work?". – n. m. could be an AI May 03 '23 at 15:42
  • @n.m. Not true. Some of the suggestions are objectively better than the others. It isn't at all what `*((*this).mem + 0)` does, for example. – FreelanceConsultant May 04 '23 at 14:10
  • It is still impossible to provide a single authoritative answer to this question even if some of the suggestions are better (by what metric?) than some others. – n. m. could be an AI May 04 '23 at 14:23
  • See [here](https://stackoverflow.com/help/dont-ask). "Your answer is provided along with the question, and you expect more answers". – n. m. could be an AI May 04 '23 at 14:31
  • @n.m. And yet, the suggestions, which are not "answers" are wrong. – FreelanceConsultant May 06 '23 at 14:05

3 Answers3

6

You could say (*this)[0].

There's nothing stopping you from using mem[0] either though, in any version of C++.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
0

Yes, this is a good approach. You can make the call more pleasing by writing it as (*this)[0].

However, if you don't mind bypassing the base class's interface (I wouldn't — I'd actually make mem private), you can write mem[0]!

If you're having trouble with that, it's due to something not in your testcase. For example, if it's a base member in a class template, you may need to write this->mem[0] due to a C++ oddity.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

Although this is mostly a matter of preference, and other answers have given some options that use (*this)[0], I tend to prefer a cleaner syntax that doesn't require use of this at all:

return operator[](0);
Riot
  • 15,723
  • 4
  • 60
  • 67