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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:28:36+00:00 2026-05-27T21:28:36+00:00

Within a C++ class hierarchy, is it possible to enforce a requirement that a

  • 0

Within a C++ class hierarchy, is it possible to enforce a requirement that a particular virtual function always call its base class’s implementation also? (Like the way constructors chain?)

I’m looking at a case where a deep class hierarchy has some common interface functions which each child overrides. I’d like each derived class’ override to chain through to the base class. It’s straightforward to do this explicitly with eg the code below, but there’s the risk that someone implementing a new derived class might forget to chain through to the base.

Is there some pattern to enforce this, such that the compiler will throw an error if an override fails to chain the base?

So, in

class CAA 
{
   virtual void OnEvent( CEvent *e ) { 
     // do base implementation stuff;
   }
}

class CBB : public CAA
{
   typedef CAA BaseClass;
   virtual void OnEvent( CEvent *e ) { 
       DoCustomCBBStuff();
       BaseClass::OnEvent( e ); // chain to base
   }
}

class CCC : public CBB
{
   typedef CBB BaseClass;
   virtual void OnEvent( CEvent *e ) { 
       Frobble();
       Glorp();
       BaseClass::OnEvent( e ); // chain to CBB which chains to CAA, etc
   }
}

class CDD : public CCC
{
   typedef CCC BaseClass;
   virtual void OnEvent( CEvent *e ) { 
       Meep();
       // oops! forgot to chain to base!
   }
}

is there a way, some template trick or syntactic gimmick, to make CDD throw a more obvious error?

  • 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-27T21:28:37+00:00Added an answer on May 27, 2026 at 9:28 pm

    The way its done is the base class method is not virtual and calls a protected virtual method.

    Of course, that only handles one level.

    In your particular situation, a large amount of infrastructure can make it work, but it’s not worth it.

    The typical response is to add a comment

    // Always call base class method
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following class hierarchy: class Base { protected: virtual void
I have a property within a class, that I need to iterate through all
Is it possible to create an inner class within an interface? If it is
When referencing class properties from a function within the class do you use the
Here's the situation. I have a base class which implements a method that accepts
I'm having some trouble calling a function which is within a class in python.
I'm working on a (rather horrible) project that involves importing a C++ class hierarchy
I have a requirement to represent a hierarchical structure in a class within c#.
VC++ makes functions which are implemented within the class declaration inline functions. If I
I'm trying to use a class within a T4 template in VS2008. Here is

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.