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

  • Home
  • SEARCH
  • 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 8235605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:51:59+00:00 2026-06-07T18:51:59+00:00

#include <iostream> struct B1 { virtual void method()=0; virtual ~B1(){} }; struct B2 {

  • 0
#include <iostream>
struct B1
{
    virtual void method()=0;
    virtual ~B1(){}
};

struct B2
{
    virtual void method()=0;
    virtual ~B2(){}
};

struct D: B1, B2
{
    virtual void method()
    {
        std::cout << "D::method\n";
    };
};

int main(int argc,char *argv[])
{
    D d;
    B1 &b1=d;
    B2 &b2=d;
    b1.method();
    b2.method();
    return 0;
}

Note, B1 and B2 do not share common interface.

Is it this legal? If yes – in which standard? C++98/03/11 ?

Both, msvc and gcc have compiled it OK.

Previously I thought, that I have to use some common interface for such case (possible virtual inheritence).

Does such situation have some special name?

How it works in details, please? Maybe some ISO references?

  • 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-06-07T18:52:01+00:00Added an answer on June 7, 2026 at 6:52 pm

    Your code is well-formed: void D::method() overrides both void B1::method() and void B2::method().

    The specification states (C++11 §10.3/2):

    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, 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 overrides Base::vf.

    B1 declares a virtual member function void B1::method(). Class D is derived from B1 and it also declares a member function with the same name (method), the same parameter list (no parameters), the same cv-qualification (no qualification) and the same ref-qualifier (no qualification).

    Therefore, void D::method() overrides void B1::method().

    The same logic applies for void B2::method() (just substitute B2 for B1 in the above explanation), so void D::method() overrides both void B1::method() and void B2::method().

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

Sidebar

Related Questions

Consider: #include <iostream> using namespace std; struct A { virtual void f() { cout
#include<iostream> #include<string> using namespace std; int main(void) { struct STRCT { int num; string
Having the following code: #include <iostream> struct A { int x; A(){} ~A(){std::cout <<~A(<<x<<)\n;}
#include <iostream> using namespace std; struct Node { char item; Node *next; }; void
#include <iostream> using namespace std; struct Node { char item; Node *next; }; void
#include <iostream> using namespace std; struct Y; struct X { X(const Y&) { cout
#include <iostream> using namespace std; struct list { char name[20]; int age; double height;
#include <iostream> using namespace std; int main() { struct list { string name; int
#include<iostream> using namespace std; struct data { int x; data *ptr; }; int main()
#include<iostream> template<class T> struct Foo { T v_; Foo(T&& v):v_(std::forward<T>(v)) { std::cout << building

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.