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 7627091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:24:49+00:00 2026-05-31T05:24:49+00:00

In C++ why the pure virtual method mandates its compulsory overriding only to its

  • 0

In C++ why the pure virtual method mandates its compulsory overriding only to its immediate children (for object creation), but not to the grand children and so on ?

struct B {
  virtual void foo () = 0;
};
struct D : B {
  virtual void foo () { ... };
};

struct DD : D {
  // ok! ... if 'B::foo' is not overridden; it will use 'D::foo' implicitly
};

I don’t see any big deal in leaving this feature out.
For example, at language design point of view, it could have been possible that, struct DD is allowed to use D::foo only if it has some explicit statement like using D::foo;. Otherwise it has to override foo compulsory.

Is there any practical way of having this effect in C++?

  • 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-31T05:24:50+00:00Added an answer on May 31, 2026 at 5:24 am

    I found one mechanism, where at least we are prompted to announce the overridden method explicitly. It’s not the perfect way though.

    Suppose, we have few pure virtual methods in the base class B:

    class B {
      virtual void foo () = 0;
      virtual void bar (int) = 0;
    };
    

    Among them, suppose we want only foo() to be overridden by the whole hierarchy. For simplicity, we have to have a virtual base class, which contains that particular method. It has a template constructor, which just accepts the type same as that method.

    class Register_foo {
      virtual void foo () = 0; // declare here
      template<typename T>  // this matches the signature of 'foo'
      Register_foo (void (T::*)()) {}
    };
    class B : public virtual Register_foo {  // <---- virtual inheritance
      virtual void bar (int) = 0;
      Base () : Register_foo(&Base::foo) {}  // <--- explicitly pass the function name
    };
    

    Every subsequent child class in the hierarchy would have to register a foo inside its every constructor explicitly. e.g.:

    struct D : B {
      D () : Register_foo(&D::foo) {}
      virtual void foo () {};
    };
    

    This registration mechanism has nothing to do with the business logic. Though, the child class can choose to register using its own foo or its parent’s foo or even some similar syntax method, but at least that is announced explicitly.

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

Sidebar

Related Questions

I have an abstract class defining a pure virtual method in c++: class Base
When implementing polymorphic behavior in C++ one can either use a pure virtual method
In object-oriented paradigm, a virtual function or virtual method is a function or method
duplicate of: "pure virtual method called" when implementing a boost::thread wrapper interface I am
Which virtual table will be pure virtual function located? In the base class or
I want a pure virtual parent class to call a child implementation of a
I have an abstract class with a pure virtual function f() and i want
What is the difference between a pure virtual function and a virtual function? I
I have a function X() declared as PURE VIRTUAL in base class: class Base
One of my C# Winforms applications randomly displays a pure virtual function call message

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.