1

There's a class below

class A
{
    public:
        string& getStr()
        {
          // Do a lot of work to get str
          return str
        }
        const string& getStr() const;
};

Can I call the first function within the second function? I want to do this, because the second function have a lot of code in common with the first one. It cannot be like this:

const string& A::getStr() const
{
    // Wrong
    A temp;
    return temp.getStr();
}

because there is added a new temp and the inner status between *this and temp is different (*this != temp).

Can it be called like I described?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
if_
  • 13
  • 4
  • 1
    Have a look at http://stackoverflow.com/questions/123758/how-do-i-remove-code-duplication-between-similar-const-and-non-const-member-func – Sander De Dycker Oct 03 '13 at 15:12
  • 1
    A side comment. If `str` is a local variable (as it looks like from the snippet above) then `string& getStr()` is returning a reference to a local variable. Don't do this! Return by value instead. – Cassio Neri Oct 03 '13 at 15:12
  • There's a bit of a code smell there... what is `// lot of work`? Does this work update a member of the object and then you return a reference to it? Does it create a local variable and update it? In either case, the design looks wrong – David Rodríguez - dribeas Oct 03 '13 at 15:21

4 Answers4

2

As mentioned in How do I remove code duplication between similar const and non-const member functions?, the solution to avoid code duplication would be to put logic in the const method (assuming you do not need to modify object state or member that you modify is mutable) and call a const method from non const one:

class A
{
    public:
      string& getStr()
      {
            return const_cast<string&>( static_cast<const A *>( this )->getStr() );
      }

      const string& getStr() const {
        // Do a lot of work to get str
        return str
      }
};
Community
  • 1
  • 1
Slava
  • 43,454
  • 1
  • 47
  • 90
  • I use `template T const& as_const( T& foo ) { return foo; }` and `return const_cast( as_const(*this).getStr() );` to reduce the amount of explicit casting in the above. – Yakk - Adam Nevraumont Oct 03 '13 at 17:03
0

By making a method const you are making a promise (loosely) to the consumer that the method will not make any changes to the object. If your non-const version makes changes to the object then it cannot be called in the const method. It doesn't matter whether or not the current object is const or not. If your non-const method does not make changes to the object, then make it const.

Will Custode
  • 4,576
  • 3
  • 26
  • 51
0

getStr() const cannot call getStr() (non-const).

const functions can be called by non-const functions, however. Happens all the time. This is because a const function can only be called with a const this pointer. In a non-const function, the this pointer is also non-const, but it can be easily cast to a const pointer. The reverse is not true -- you can't (easily/correctly) cast away the constness of a const pointer. So from within a nonconst method you can call other, const methods.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • I'm assuming the downvote was for the stand that getters should be `const`? – John Dibling Oct 03 '13 at 15:35
  • Point of writing a non const getter. Imagine container, which const method returns const reference and non const method non const reference, but logic is the same. Good enough? – Slava Oct 03 '13 at 15:35
  • @Slava: OK, fair enough. – John Dibling Oct 03 '13 at 15:36
  • @Slava: But that isn't what OP appears to be wanting, here. Something still doesn't feel right. – John Dibling Oct 03 '13 at 15:37
  • it is difficult to say what OP really wants, code is too short. I think we should assume that he has reasoning to do what he wants unless it is clear from the code that he does something wrong. Thanks for fixing your answer. – Slava Oct 03 '13 at 15:41
-1

The function getStr in your class A returns a reference of a local variable. What's more, it is the same question to the second function.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
chace hu
  • 1
  • 2