1

I have two classes representing contexts for some methods.

class BaseContext {}; class DerivedContext : public BaseContext {}; 

I have a base class:

class MyBase { protected: virtual void doSome(BaseContext* context); }; 

And a derived class:

class MyDerived : public MyBase { protected: virtual void doSome(DerivedContext* context) override; // Overriding virtual void doSome(DerivedContext* context); // Overloading? }; 

Since DerivedContext derives from BaseContext, it might seems that I am overriding doSome. But it might also be an overloading...

  1. Which one is correct? Am I overriding or overloading here?
  2. Thus, if I type (MyBase* my = new MyDerived())->doSome(new DerivedContext()), what should I get?
2
  • Have a look at stackoverflow.com/questions/7181361/… Commented Aug 26, 2016 at 8:59
  • Your derived type has two identical methods - override doesn't change the type - which isn't allowed. Was one of them supposed to take a BaseContext argument? Commented Aug 26, 2016 at 8:59

3 Answers 3

4

This is neither overriding nor overloading. Since the parameter's type is different, MyDerived::doSome is just hiding MyBase::doSome.

1.1. Since DerivedContext derives from BaseContext, it might seems that I am overriding doSome.

No. Here're the preconditions of overriding listed in the standard. $10.3/2 Virtual functions [class.virtual]:

(emphasis mine)

If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list ([dcl.fct]), cv-qualification, and ref-qualifier (or absence of same) as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides110 Base::vf.

110) A function with the same name but a different parameter list (Clause [over]) as a virtual function is not necessarily virtual and does not override.

In fact with override specifier you'll get a compile error for this case. e.g.

error: 'doSome' marked 'override' but does not override any member functions 

1.2. But it might also be an overloading...

You can't overload functions across scopes according to the rule of unqualified name lookup (unless using using-declaration to introduce names into the same scope).

  1. Thus, if I type (MyBase* my = new MyDerived())->doSome(new DerivedContext()), what should I get?

MyBase::doSome() will be invoked since you call it on a MyBase*. This is not overriding, so no dynamic dispatching happens here.

LIVE

Note the argument DerivedContext* will be implicitly converted to BaseContext* and then passed to the function. BTW (MyBase* my = new MyDerived())->... is not valid syntax.

Sign up to request clarification or add additional context in comments.

8 Comments

I dont think its hidden. u can still call doSome(BaseContext*) as well.
@WikiWang You'll can't call it on MyDerived. See rextester.com/HMLR15127
@WikiWang you have to add using MyBase::doSome; to MyDerived to bring back the base overload.
@songyuanyao Well, the origin code marks those function protected, and you can call it within MyDerived::some_function using MyBase::doSome(...).
@WikiWang Yes, you just can't call it like doSome(...), you have to call it like MyBase::doSome(...), this is so-called name hiding.
|
1

While only specifying the member function with another argument type, you are hiding the function from the base class.
With the override specifier you should get a compile error as there is no such function (with the exact same signature) in the base class.

Overriding is when redefining virtual a member function in a derived class with the exact same arguments (number and type). The override specifier is optional but encouraged as it helps identifying subtle bugs as in your case.

Overloading is when defining a function (not necessarily member of a class) with a different number or types of arguments.

1 Comment

"or the redefining a non-virtual member function in a derived class." It's still hiding, not overloading.
0

Just put override after the function definition and see if it compiles. If it compiles, it is overriding the virtual method in the base class. If it does not, it would hide the base class method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.