Reputation: 3341
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
}
//...
};
I know that get() in Derived class will hide get() and get(int x) methods inside Base class. so My question is:
1) is this consedred overloading or overriding ?
2) does making get() const in the derived class will change something about (hiding or not hiding Base class methods).
Quote from a c++ book:
"It is a common mistake to hide a base class method when you intend to override it, by forgetting to include the keyword const. const is part of the signature, and leaving it off changes the signature, and thus hides the method rather than overrides it. "
Upvotes: 0
Views: 12191
Reputation: 477040
It is neither overloading nor overriding. Rather, it is hiding.
If the other function were also visible, it would be overloading, which you can achieve with using
:
class Derived : public Base
{
public:
using Base::get;
int get();
};
Even if you declared int get() const
in the derived class, it would merely be hiding the base function, since the base function is not virtual
.
Upvotes: 8
Reputation: 498
Overloading is a function named the same, but with a different signature.
Overriding is overriding a signature that already exist.
You have just "hidden", which is neither.
A quick google search reveals this: http://users.soe.ucsc.edu/~charlie/book/notes/chap7/sld012.htm which you may find helpful.
Upvotes: 2
Reputation: 272497
Neither, it's just "hiding".
No. Hiding occurs based on function name, not on function signature.
Your quote is only relevant if you've declared the base-class functions virtual
; you cannot override a non-virtual
function.
Upvotes: 2