0

As stated in the booking I'm reading:

"In order to be able to invoke get() also, if we only have a pointer or reference to a const TextureHolder at hand, we need to provide a const-qualified overload. This new member function returns a reference to a const sf::Texture, therefore the caller cannot change the texture. ... "

And then the author goes onto provide this example of the overloaded function definition:

const sf::Texture& TextureHolder::get(...) const;

I understand that if you have a reference or pointer to a const TextureHolder, you cannot modify it. Thus the return type of const sf::Texture&.

But why the appended const at the end of the function definition? Isn't that to only have a pointer to a constant this, so you cannot modify class member variables? So what purpose does that serve if our function isn't trying to modify any member/object variables?

For the curious, here is the full function:

sf::Texture& TextureHolder::get(Textures::ID id){
    auto found = mTextureMap.find(id);
    return *found->second;
}

~~~~~~~~~~

Being new to the language, I'm still getting over the million (I know, exaggerating a bit there) different uses of const in the C++ language.

Justin
  • 400
  • 1
  • 5
  • 16

3 Answers3

2

There is one notable difference. const instances will call the const overload and vice versa.

class T
{
public:
    int get() const
    {
        std::cout << "const.\n";
        return 42;
    }

    int get()
    {
        std::cout << "non-const.\n";
        return 42;
    }
};

int main()
{
    const T c;
    T t;
    auto i = c.get();
    auto j = t.get();
}
1

But why the appended const at the end of the function definition? It is the c++ syntax that has been defined to declare a const function, const at the end of the functions says that this function will not modify and member variables of this object.

Isn't that to only have a pointer to a constant this, so you cannot modify class member variables? Each time a member function is called a this parameter is pushed on the stack by the compiler, you are not declaring this variable in the parameter list so that you can make it const, so this is the way to do it.

So what purpose does that serve if our function isn't trying to modify any member/object variables? If you are sure your function is not going to change the member variables you can make the function const, because in case in future any changes are made to the function (may be not by you), it confirms that the person changing the function does not change the member variables, if changed, it can be caught at the compile time.

51k
  • 1,381
  • 3
  • 12
  • 22
1

You should consider this to be an additional argument passed to member functions. The const at the end of the member function indicates that the argument can be const qualified. Without a const qualified overload you can't call the member function on a const object. During overload resolution the appropriate member is chosen:

sf::Texture       t = ...;
sf::Texture const ct = ...;

t.get();  // calls sf::Texture::get()
ct.get(); // calls sf::Texture::get() const
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380