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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:57:52+00:00 2026-05-13T20:57:52+00:00

a more exact version of the code is: class SomeParam; class IBase { public:

  • 0

a more exact version of the code is:

class SomeParam;
class IBase
{
public:
    virtual void Func(SomeParam* param = NULL)
    {
        cout << "Base func";
    }
};

class DerivedA : public IBase
{
public:
    void Func()
    {
        //do some custom stuff
        cout << "DerivedA func";
        IBase::Func();
    }
};

class DerivedB : public IBase
{
public:
    void Func()
    {
        //do some custom stuff
        cout << "DerivedB func";
        IBase::Func();
    }
};

//at somewhere else
void FuncCaller(IBase *instance1, IBase *instance2)
{
    IBase *i1 = instance1;
    IBase *i2 = instance2;
    i1->Func();
    i2->Func();
}

DerivedA *a = new DerivedA;
DerivedB *b = new DerivedB;
FuncCaller(a,b);

This gives me:
“Base func”
“Base func”

  • 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-13T20:57:52+00:00Added an answer on May 13, 2026 at 8:57 pm

    It looks like you have provided a simplified version of your code to make it more readable, but you have oversimplified inadvertently.

    The most likely reasons for your behaviour are:

    • Slicing in FuncCaller() (see quamrana’s answer for the details)
    • Wrong overriding, such as making the derived class function const, while the base class function is not const

    EDIT: After reading the edited question, it is clearly the second reason. You are not overriding the base class function, but hiding it in the derived classes with a new definition. You need to keep exactly the same signature (covariance does not apply here, since the function returns void) in the derived classes. In code, you need to do either:

    class SomeParam;
    class IBase
    {
    public:
        virtual void Func(SomeParam* param = NULL)
        {
            cout << "Base func";
        }
    };
    
    class DerivedA : public IBase
    {
    public:
        void Func(SomeParam* param = NULL)
        {
            //do some custom stuff
            cout << "DerivedA func";
            IBase::Func();
        }
    };
    
    class DerivedB : public IBase
    {
    public:
        void Func(SomeParam* param = NULL)
        {
            //do some custom stuff
            cout << "DerivedB func";
            IBase::Func();
        }
    };
    
    //at somewhere else
    void FuncCaller(IBase *instance1, IBase *instance2)
    {
        IBase *i1 = instance1;
        IBase *i2 = instance2;
        i1->Func();
        i2->Func();
    }
    
    DerivedA *a = new DerivedA;
    DerivedB *b = new DerivedB;
    FuncCaller(a,b);
    

    or

    class SomeParam;
    class IBase
    {
    public:
        virtual void Func()
        {
            cout << "Base func";
        }
    };
    
    class DerivedA : public IBase
    {
    public:
        void Func()
        {
            //do some custom stuff
            cout << "DerivedA func";
            IBase::Func();
        }
    };
    
    class DerivedB : public IBase
    {
    public:
        void Func()
        {
            //do some custom stuff
            cout << "DerivedB func";
            IBase::Func();
        }
    };
    
    //at somewhere else
    void FuncCaller(IBase *instance1, IBase *instance2)
    {
        IBase *i1 = instance1;
        IBase *i2 = instance2;
        i1->Func();
        i2->Func();
    }
    
    DerivedA *a = new DerivedA;
    DerivedB *b = new DerivedB;
    FuncCaller(a,b);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Somehow my code doesn't work any more (it did work before with the exact
Here is a much more short version of my code: char token[256]; unsigned int
I'm implementing a UIView's (UITableViewCell to be more exact) drawRect method. My view has
I recently started working with MVC or MVC2 to be more exact. I found
To be more specific I do not know what the exact values will be.
More than one time I have encountered the following problem when building code with
People generally agree that C# and VB.net are more or less the exact same
The C libraries have a nice form of late binding, where the exact version
[EDITED: I left the original question below, with some more context and code to
More C++ learning questions. I've been using vectors primarily with raw pointers with a

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.