3

There is somewhat odd sample given in one of the Microsoft documentation pages , which consists of two classes, one is a base class and another one is a derived. The base class has the following virtual function member:

virtual void setEars(string type)      // virtual function
{
    _earType = type;
}

And another, defined in the derived class, which, as stated in comments, redefines the virtual function:

// virtual function redefined
void setEars(string length, string type)
{
    _earLength = length;
    _earType = type;
}

These two have different signatures and I haven't ever heard if you actually can redefine a virtual function with a function of a different signature. I compiled this sample and could not find any overriding behavior between these two. Is the sample just misleading or I'm missing something?

The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49
  • Redefine is not the same as override. – Evg Nov 22 '19 at 14:10
  • 5
    That's juste one of the many broken examples on MSDN. I don't even bother looking at them anymore. – Quentin Nov 22 '19 at 14:13
  • @Evg could elaborate a little further without telling me that Microsoft means overloading here, because they clearly don't - https://learn.microsoft.com/en-us/cpp/cpp/virtual-functions?view=vs-2019 – The Dreams Wind Nov 22 '19 at 14:13
  • 1
    @Evg `_earType` is duplicated in both classes, and the comment advertises that the example "Exhibits polymorphism/virtual functions", which it doesn't. There's also a rogue `#define TRUE = 1` which makes no sense and isn't even used. – Quentin Nov 22 '19 at 14:19
  • @Quentin, found complete example here https://learn.microsoft.com/en-us/cpp/cpp/class-cpp?view=vs-2019. Yes, this example looks misleading. Agree. – Evg Nov 22 '19 at 14:21
  • 4
    Microsoft's C++ documentation is really, really bad sometimes. Like "I don't know C++ but my manager told me to write this so we meet this month's quota" bad. – molbdnilo Nov 22 '19 at 14:23
  • 1
    But at least the comment above, "virtual function redefined", is correct. It is redefined, not overridden. The `setEars` from the base class will be hidden by that redefinition. – Evg Nov 22 '19 at 14:26
  • 2
    @Evg it's still not correct, since that new function that hides the other one is *not* virtual ;) – Quentin Nov 22 '19 at 14:33
  • 1
    @Quentin, **virtual function** in the base class **redefined** with a non-virtual one. – Evg Nov 22 '19 at 14:46
  • @Evg I do have to admire your determination to salvage this trainwreck of an example :D – Quentin Nov 22 '19 at 14:49
  • @Quentin, I'm just trying to squeeze at least a little bit of sense out of it. :) – Evg Nov 22 '19 at 14:51
  • Nowadays MS have their Visual Studio documentation on [github](https://github.com/MicrosoftDocs/cpp-docs) so it's pretty easy to propose changes. I think I got the last `#include ` out of the documentation that way :-) – Ted Lyngmo Nov 22 '19 at 14:53

1 Answers1

8

Is the sample just misleading or I'm missing something?

This example is indeed misleading. When overriding a virtual function in a derived type, it must come with the same signature as it is defined in the base class. If that is not the case, the function in the child class will be considered as its own entity and is not considered in a polymorphic function call. Additionally, it will hide the name of the base classes function, which is considered bad practice, as it violates the "is-a" relationship in public inheritance.

In order to prevent such accidental hiding, C++ introduced the override keyword. When overriding a virtual function, it then must have a matching signature, otherwise, the compiler will reject it.

Jodocus
  • 7,493
  • 1
  • 29
  • 45
  • 4
    Another motivating reason for the ```override``` proposal was when a base class signature changed, and previously overriding functions suddenly stopped overriding it, silently switching into an introduction of a new (possibly non-virtual) function. Using ```override``` makes catching those situations trivial. – Chris Uzdavinis Nov 22 '19 at 14:40