Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ override virtual function with const parameter instead of non-const parameter

When I was writing an overide function with const parameter instead of non-const parameter, I thought the compiler would report an error, because the base function has non-const parameter, but it succeeded to compile it. Why?

My code:

#include <iostream>

class A
{
public:
    virtual uint64_t cal(uint64_t value)
    {
        std::cout << value << std::endl;
        return value;
    }
};
class B : public A
{
public:
    uint64_t cal(const uint64_t value) override;
};
uint64_t B::cal(const uint64_t value)
{
  std::cout << value + 1 << std::endl; 
  return (value+1);
}
int main()
{
    B b;
    b.cal(1);
    return 0;
}
like image 322
Carol Avatar asked Mar 04 '20 07:03

Carol


2 Answers

Why?

Because the top const qualifier of a function argument is ignored in a declaration.

uint64_t cal(uint64_t value);

and

uint64_t cal(const uint64_t value);

declare the exact same type of function. cal is a function taking a uint64_t and returning a uint64_t. The const qualifier makes no difference for calling code, since value is always a copy of the passed in argument.

The only difference is in the function body, where the const qualifier will prevent you from modifying the paraemter. But that is an implementation detail.

This difference can even raise coding style questions. For instance see

Is it better to remove "const" in front of "primitive" types used as function parameters in the header?

like image 175
StoryTeller - Unslander Monica Avatar answered Sep 28 '22 08:09

StoryTeller - Unslander Monica


Note that top-level cv-qualifiers are dropped when considering function type, so uint64_t cal(uint64_t) and uint64_t cal(const uint64_t) are considered as the same function type.

(emphasis mine)

The type of each function parameter in the parameter list is determined according to the following rules:

4) Top-level cv-qualifiers are dropped from the parameter type (This adjustment only affects the function type, but doesn't modify the property of the parameter: int f(const int p, decltype(p)*); and int f(int, const int*); declare the same function)

like image 38
songyuanyao Avatar answered Sep 28 '22 08:09

songyuanyao