AlexDan
AlexDan

Reputation: 3341

overriding a const method of the Base class from the derived 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
     }
      //...

};

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

Answers (3)

Kerrek SB
Kerrek SB

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

Edward
Edward

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

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272497

  1. Neither, it's just "hiding".

  2. 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

Related Questions