Search code examples
c++constantsvirtual

What is the difference between const virtual and virtual const?


I saw that some function in C++ was declared as

virtual const int getNumber();

But what is the difference if the function is declared as the following?

const virtual int getNumber();

What is the difference between those two?


Solution

  • As was already said, there is no difference. However, note that these two do differ:

    virtual const int getNumber();
    virtual       int getNumber() const;
    

    In the first method, const refers to the returned value of type int.

    In the second method, const refers to the object the method is called on; that is, this will have type T const * inside this method, - you will be able to call only const methods, modify only mutable fields and so on.