小编Car*_*rol的帖子

c ++用const参数而不是非常量参数覆盖虚函数

当我在写一个带有const参数而不是非常量参数的overide函数时,我以为编译器会报错,因为base函数有非常量参数,但是编译成功了。为什么?

我的代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

c++ overriding constants signature

5
推荐指数
2
解决办法
330
查看次数

标签 统计

c++ ×1

constants ×1

overriding ×1

signature ×1