1

I have a C++ class that is used as a function parameter which is causing me some grief. I can't seem to call function from the class parameter when it is designated as const.

sample class:

class FooBar
{
  private:
    bool barFoo; // I want this modifiable only through functions

  public:
    const bool& getBarFoo() // this function needs to be used to retrieve it
    { 
      return barFoo;
    }

    void setBarFoo(bool newVal) // this is the set function. It validates if the set is possible
    {
      // check if modification is possible. 
      barFoo = newVal
    }
}

I try to use this class in a function similar to this:

void DoShizzle(const FooBar& yesOrNo)
{
  if(yesOrNo.getBarFoo()) // here the code shows an error*
  {
    // do shizzle
  }
  else
  {
    // do other shizzle
  }
}

* the message says 'the object has type qualifiers that are not compatible with the member function "FooBar::getBarFoo" object type is: const FooBar'.

If I remove the 'const' directive from the FooBar parameter in the DoShizzle function, the error goes away. However I read that you should try to tell developers and the compiler what you're doing. I want to keep the variable barFoo private so that it can only be modified using the setBarFoo function, to prevent it being modified without validating if it can be. But I also want to communicate that the function DoShizzle will not edit the FooBar class.

What can is a good solution to my problem? Live with the fact I can't use const here? Or am i missing another solution to my problem? I'm fairly new to C++ and a C# developer by nature so I know I might have to unlearn some practices

martijn
  • 1,417
  • 1
  • 16
  • 26
  • 1
    You need to make the member function `getBarFoo()` a `const` member function. Also, generally use should not return a data member by reference. Instead return it **by value**. – Jason Jan 24 '22 at 12:58
  • Also see [this presentation](https://youtu.be/O65lEiYkkbc?t=1919) on const correctness. – JHBonarius Jan 24 '22 at 13:08
  • Does this answer your question? [const overloaded operator\[\] function and its invocation](https://stackoverflow.com/questions/5762042/const-overloaded-operator-function-and-its-invocation) – JHBonarius Jan 24 '22 at 13:09
  • @JHBonarius that answer is still beyond what i understand. thanks for the presentation, I'll be sure to watch it. – martijn Jan 24 '22 at 13:21
  • Yeah overloading in C++ is an advanced topic. But very important though. – JHBonarius Jan 24 '22 at 14:01

2 Answers2

4

const qualifier of a member function is not necessarily the same as the const qualifier of its return type. Actually they are unrelated in general (only when you return a reference to a member, from a const method one can only get a const reference). Your method:

const bool& getBarFoo() // this function needs to be used to retrieve it
{ 
  return barFoo;
}

Is a non-const method that returns a const bool& (a constant reference to a bool).

If you want to be able to call the method on a const FooBar you must declare the method as const:

const bool& getBarFoo() const // <--------
{ 
  return barFoo;
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

The problem is that currently the member function getBarFoo cannot be used on const objects of type FooBar.

To solve this problem you should make the member function getBarFoo a const member function by adding const as shown below:

class FooBar
{
  private:
    bool barFoo; 

  public:
    ////////////////////////VVVVV
    const bool& getBarFoo() const //added const here
    { 
      return barFoo;
    }

    void setBarFoo(bool newVal) 
    {
       
      barFoo = newVal;
    }
};
Jason
  • 36,170
  • 5
  • 26
  • 60