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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:31:32+00:00 2026-05-21T07:31:32+00:00

guys, I have a case that needs the child class needs to call its

  • 0

guys,
I have a case that needs the child class needs to call its parent virtual function at first before call its override virtual function.

BaseClass::Draw()
{

}

ChildClass::Draw()
{
    BaseClass::Draw(); // BaseClass Draw must be called first.
}

GrandChildClass::Draw()
{
    ChildClass::Draw(); // ChildClass Draw must be called first.
}

I want to hide this behavior from clients. Is there pattern on this?

Thanks.

  • 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-21T07:31:32+00:00Added an answer on May 21, 2026 at 7:31 am

    For simple cases you can use a second, private member function for the overrideable behavior:

    class Base {
    public:
        void Draw() { 
            // Base-class functionality here
            OverrideableDraw();
        }
    
    private:
        virtual void OverrideableDraw() { }
    };
    
    class Derived : public Base {
    private:
        virtual void OverrideableDraw() {
            // Derived-class functionality here
        }
    };
    

    For more complex hierarchies (e.g. where you have multiple levels of inheritance), this isn’t possible since any derived class can override any virtual member function (there is no final in C++). Usually it is safe to assume that each derived class is doing the right thing. While I can think of a few times that I’ve run into issues because a derived class screwed up overriding, those cases were usually pretty straightforward to debug.

    If you are really worried about it and really want to guarantee that base-class overrides are executed first, you could use something like this, though this is quite expensive (at least this naive implementation is quite expensive):

    #include <functional>
    #include <iostream>
    #include <vector>
    
    class Base {
    public:
    
        Base() {
            RegisterDrawCallback(std::bind(&Base::DrawCallback, this));
        }
    
        void Draw() {
            for (auto it(drawCallbacks_.begin()); it != drawCallbacks_.end(); ++it)
                (*it)();
        }
    
    protected:
    
        typedef std::function<void(void)> DrawCallbackType;
        typedef std::vector<DrawCallbackType> DrawSequence;
    
        void RegisterDrawCallback(DrawCallbackType f) {
            drawCallbacks_.push_back(f);
        }
    
    private:
    
        void DrawCallback() { std::cout << "Base" << std::endl; }
    
        DrawSequence drawCallbacks_;
    };
    
    class Derived : public Base {
    public:
    
        Derived() {
            RegisterDrawCallback(std::bind(&Derived::DrawCallback, this));
        }
    
    private:
    
        void DrawCallback() { std::cout << "Derived" << std::endl; }
    };
    
    class DerivedDerived : public Derived {
    public:
    
        DerivedDerived() {
            RegisterDrawCallback(std::bind(&DerivedDerived::DrawCallback, this));
        }
    
    private:
    
        void DrawCallback() { std::cout << "DerivedDerived" << std::endl; }
    };
    

    [This is just one option; someone else can probably come up with a far more elegant solution. Personally, I’d just make sure the virtual member functions are well-documented and leave it at that.]

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

Sidebar

Related Questions

Hey guys I have an admin page that checks if you are admin before
hi guys i have two pipe delimited files,first file contains 1000 records and second
Platform: C# 2.0 WinForms I have a factory class that provides an instantiation of
I have a form that needs to allow people to search for hikes, hiking
hey guys, i did the following function in order to have the same height
I have a table (lets call it main_guys) that looks like: id, other_item_id, other_item_type,
do you guys have any idea how to edit the the labels in the
Thus far you guys have been wildly helpful with me getting this little ditty
When reading about SQL Injection and XSS i was wondering if you guys have
Python list comprehensions are nice, but near impossible to debug. You guys have any

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.