Ale*_*Dan 0 c++ overriding overloading class
class Base{
//...
public:
int get()const{ // const
// Do something.
}
int get(int x){
// Do Someting.
}
//...
};
class Derived:public Base{
//....
public:
int get(){ // not const (not the same signature as the one is the base class)
//Dosomething
}
//...
};
Run Code Online (Sandbox Code Playgroud)
我知道Derived类中的get()将隐藏Base类中的get()和get(int x)方法.所以我的问题是:
1)这是否意外超载或覆盖?
2)在派生类中使get()const会改变一些东西(隐藏或不隐藏Base类方法).
从c ++书中引用:
"当你打算覆盖它时,通过忘记包含关键字const来隐藏基类方法是一个常见的错误.const是签名的一部分,而离开它会改变签名,因此隐藏方法而不是覆盖它."
既不是超载也不是压倒一切.相反,它隐藏着.
如果其他功能也可见,则会出现重载,您可以通过以下方式实现using:
class Derived : public Base
{
public:
using Base::get;
int get();
};
Run Code Online (Sandbox Code Playgroud)
即使你int get() const在派生类中声明,它也只是隐藏了基函数,因为基函数不是virtual.