Home > Software engineering >  C : How to create getters/setters for a pointer to a pointer to an array of 3 doubles
C : How to create getters/setters for a pointer to a pointer to an array of 3 doubles

Time:12-09

I have a question of how to return a pointer to a pointer to an array of 3 doubles. In my example, I have a class that has as private member:

class MyColorClass {
....
    private:
    const double (**colorData)[3];
...
}

How should I write the getter and the setter for this?

This does not work:

const double *** MyColorClass::getcolorData()
{
    return colorData;
}

PS: I apologize for a previous badly written similar question (I deleted it)

Thank you :)

CodePudding user response:

const double (**getColorData() const)[3]
{
    return colorData;
}

The const on the right isn't specific to this type, and should be added to all getters. Normally it's on the very right of the declaration, but not in this case.

Replacing array with pointer like you did only makes sense if you're making a getter for an array, but colorData is a pointer, not an array (doesn't matter what it's a pointer to).


                getColorData              // getColorData is
                getColorData() const      // a const function, returning
               *getColorData() const      // a pointer to
              **getColorData() const      // a pointer to
             (**getColorData() const)     // ...
             (**getColorData() const)[3]  // an array of 3 of
const double (**getColorData() const)[3]  // const double
  • Related