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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:50:43+00:00 2026-06-11T08:50:43+00:00

Overwrite a derived function behaviour while still calling the same function of it’s parent,

  • 0

Overwrite a derived function behaviour while still calling the same function of it’s parent, see C::foo() for example.It still have the behaviour of A::foo but extended it. Is it too simple to be a pattern ?

class A
{
public:
    virtual ~A() {}
    virtual void foo(){ MSG("A::foo");}
};

class B : public A
{
public:
    virtual void foo(){ MSG("B::foo");}
};

class C : public B
{
public:
    virtual void foo(){ 
        A::foo(); //here still have the behaviour of A::foo but extended it
        MSG("C::foo");
    }
};

void callFoo(A* p)
{
    p->foo();
}

int main()
{
    boost::scoped_ptr<A> myPtr(new A());
    callFoo(myPtr.get());

    myPtr.reset(new B());
    callFoo(myPtr.get());

    myPtr.reset(new C());
    callFoo(myPtr.get());

    return 0;
}
  • 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-11T08:50:44+00:00Added an answer on June 11, 2026 at 8:50 am

    There’s no requirement for a design element to be complicated to be called a “pattern”.

    However, there is a requirement that it be useful; that it makes it easier to produce correct, coherent, efficient and flexible designs. Implementing useful functionality in a base class, and then placing a burden on derived classes to remember to invoke it manually, doesn’t really meet that requirement, and so I’d be tempted to label it an “anti-pattern”. Adding an intermediate base class, and expecting derived class implementors to figure out which override(s) they actually want, is more along the lines of a “confusing mess”.

    If you want a bona fide pattern (described using capital letters in a famous book) for this situation, then the Template Method might be appropriate – the base class contains a non-virtual function which performs some common functionality, delegating part of the work to private, pure virtual functions that can be overridden. Personally, I’d go one step further and delegate to a separate class, since I like to give each class a single responsibility and avoid inheriting from anything except abstract interfaces, giving something along the lines of:

    class FooInterface {
    public:
        virtual ~FooInterface() {}
        virtual void foo() = 0;
    };
    
    class A {
    public:
        void foo(FooInterface & impl) {
            MSG("A::foo"); // Happens whatever the implementation does
            impl.foo();
        }
    };
    
    class C : public FooInterface {
        virtual void foo() {
            // We don't have to worry about A at all
            MSG("C::foo");
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish best understand the difference between dealloc and release function.... example... I have
Bar and Box are derived classes of Foo and Foo has a virtual function
I have C++ class with multiple parents; each parent defines a function with a
I have two objects that are derived from same the base class. Lets say
Is it possible in C++ to have a class override a virtual function, but
I want to override a function with same parameter type, but with different logical
We have a memory overwrite problem. At some point, during the course of our
Is it possible to overwrite some HashSet element (not necessarily the one iterated) while
I have two classes, derived from a common class. The common class has a
If I derive a class from another one and overwrite a function, I can

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.