Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4247958
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:10:58+00:00 2026-05-21T04:10:58+00:00

I am writing some templated pure virtual base classes that are multiply inherited and

  • 0

I am writing some templated pure virtual base classes that are multiply inherited and discovered a minor oddity in the process. The crux of it is, if you define the same method in two base classes, inheriting from both compiles and works fine and it appears that you only need a single definition in the derived class. I am curious as to what is going on behind the scenes here, is this correct and planned behaviour or a dangerous compiler oversight?

See below for illustrative code sample:

namespace
{
  template <typename T_NumType>
  class InheritFrom
  {
  public:
    virtual void doSomething(const T_NumType& numType) = 0;

    virtual void sharedMethod() = 0;

  }; // class

  class MultipleInheritor : public InheritFrom<int>, public InheritFrom<float>
  {
  public:
    void doSomething(const int& numType) {}
    void doSomething(const float& numType) {}

    void sharedMethod() {} // one definition here

  }; // class

}

int main(int argc, char** argv)
{
  MultipleInheritor mult;
  mult.doSomething(5);
  mult.sharedMethod();

}

EDIT:
The answers below and looking at the C++98 standard finally cleared this up for me.

From 10.3: Virtual Functions:

in any well-formed class, for each
virtual function declared in that
class or any of its direct or indirect
base classes there is a unique final
overrider that overrides that function
and every other overrider of that
function.

So this means that for any virtual function a single final overrider will be found. This is done through the rules detailed in 10.2: Member Name Lookup.

So in the case I have presented there are indeed two base class functions and due to member name lookup the single function in the derived class is determined as the final overrider for both. So what I have is perfectly well formed and a logical result of the workings of the C++ compiler.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:10:59+00:00Added an answer on May 21, 2026 at 4:10 am

    The functions have different signatures (one takes an int, and one takes a float), and as such are not really “the same”.

    The template magic obfuscates that a little bit, but it’s the same as if you had foo(int) and foo(float) – those are two different functions. You could have foo(int) const and foo(int), those would be two different functions as well.

    EDIT: Okay, let me address the actual question (as outlined in your comment). It’s technically still not ambiguous: Each version of InheritFrom has its own vtable, and MultipleInheritor has one vtable. You can choose to scope any parent implementation of sharedMethod (like InheritFrom<int>::sharedMethod()), but as far as a caller is concerned, your object of type MultipleInheritor has one vtable with one entry for sharedMethod.

    EDIT EDIT: The key is that you’re implementing sharedMethod in your subclass. If it wasn’t pure and there was no implementation in MultipleInheritor, there WOULD be a compiler error since it wouldn’t be clear what to put into the one slot in the vtable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some data acquisition software and need a gui plotting library that is
So I was writing some code today that basically looks like this: string returnString
I was writing some Unit tests last week for a piece of code that
I have an abstract class defining a pure virtual method in c++: class Base
I'm writing a templated C++ generic container class that can optionally maintain its contents
I'm writing some template classes for parseing some text data files, and as such
We are writing an application that compiles with both gcc and Visual C++. Some
I have some heavily templated c++ code that I am working with. I can
I am writing some code generation templates that will work hand-in-hand with a SubSonic
I'm currently writing some jQuery-Stuff that works a lot with js-generated HTML. Since I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.