Standard C++ polymorphic virtual function at different levels of class hierarchy

IgnorantiaLegis-2839 21 Reputation points
2021-03-09T11:02:02.697+00:00

Using MS Visual Studio Community 2019, 16.9.0

First step. Excerpt from the project, no polymorphism, works fine:

class MyWindow {
protected:
virtual void ProcessChar(char chChar, int iRepeat) { /* ...does not matter... */ }
}

class MyDialogControl : public MyWindow {
/* ...no need to define ProcessChar here, it's inherited... */
}

class MySignedEditControl : public MyDialogControl {
typedef MyDialogControl super;
protected:
virtual void ProcessChar(char chChar, int iRepeat) {
/* ...some individual stuff here .../
super::ProcessChar(chChar, iRepeat); }
}

As expected, super::ProcessChar calls MyWindow::ProcessChar.
Second step. Add a polymorphic function to MyDialogControl. Some other classes derived from MyDialogControl will need it later.

class MyDialogControl : public MyWindow {
protected:
/* ...ProcessChar(char chChar, int iRepeat) should still be inherited... */
virtual bool ProcessChar(NMCHAR*) { /* do some stuff here */ }
}

Unexpected result: compile error in class MySignedEditControl , type mismatch at line: super::ProcessChar(chChar, iRepeat) does not match with ProcessChar(NMCHAR*).

There are - at least - 3 obvious solutions. Polymorphism is not essential here, so by just renaming the new function to "ProcessCharNotification(NMCHAR*)" everything turns out fine. 2nd: in MySignedEditControl calling "Window::ProcessChar" instead of "super::ProcessChar" will allow me to keep the polymorphic function name. 3rd: Defining a redirect "MyDialogControl::ProcessChar" which does nothing than call its own "super::ProcessChar" works fine.

So my question ist not about how to find a solution. I just want to understand the mechanism - why does the polymorphic call not compile in the above example? What does the C++ standard say in this case?

Thanks in advance.

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,370 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,832 questions
0 comments No comments
{count} votes

Accepted answer
  1. Igor Tandetnik 1,106 Reputation points
    2021-03-09T13:14:04.017+00:00

    Add using MyWindow::ProcessChar; to MyDialogControl.

    Introducing a member with a given name hides members with the same name inherited from base class. Name lookup searches the scopes from the inside out, and stops as soon as it finds a matching name; further scopes are not searched. If more than one function is thus found, overload resolution is performed next.

    When you added ProcessChar to MyDialogControl, the name lookup for super::ProcessChar finds that declaration and doesn't look further, and so doesn't find MyWindow::ProcessChar. using declaration brings names from MyWindow down to MyDialogControl, and allows name lookup to find them.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.