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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:02:47+00:00 2026-05-15T07:02:47+00:00

I am trying to refactor a working code. The code basically derives an interface

  • 0

I am trying to refactor a working code. The code basically derives an interface class into a working implementation, and I want to use this implementation outside the original project as a standalone class.

However, I do not want to create a fork, and I want the original project to be able to take out their implementation, and use mine. The problem is that the hierarchy structure is very different and I am not sure if this would work. I also cannot use the original base class in my project, since in reality it’s quite entangled in the project (too many classes, includes) and I need to take care of only a subdomain of the problems the original project is.

I wrote this code to test an idea how to implement this, and while it’s working, I am not sure I like it:

#include <iostream>

// Original code is:
// IBase -> Derived1

// I need to refactor Derive2 to be both indipendet class
// and programmers should also be able to use the interface class
// Derived2 -> MyClass + IBase
// MyClass


class IBase {
public:
    virtual void printMsg() = 0;
};

///////////////////////////////////////////////////
class Derived1 : public IBase {
public:
    virtual void printMsg(){ std::cout << "Hello from Derived 1" << std::endl; }
};


//////////////////////////////////////////////////
class MyClass {
public:
    virtual void printMsg(){ std::cout << "Hello from MyClass" << std::endl; }
};

class Derived2: public IBase, public MyClass{
    virtual void printMsg(){ MyClass::printMsg(); }
};

class Derived3: public MyClass, public IBase{
    virtual void printMsg(){ MyClass::printMsg(); }
};

int main()
{
    IBase *o1 = new Derived1();
    IBase *o2 = new Derived2();
    IBase *o3 = new Derived3();
    MyClass *o4 = new MyClass();

    o1->printMsg();
    o2->printMsg();
    o3->printMsg();
    o4->printMsg();

    return 0;
}

The output is working as expected (tested using gcc and clang, 2 different C++ implementations so I think I am safe here):

[elcuco@pinky ~/src/googlecode/qtedit4/tools/qtsourceview/qate/tests] ./test1
Hello from Derived 1
Hello from MyClass
Hello from MyClass
Hello from MyClass
[elcuco@pinky ~/src/googlecode/qtedit4/tools/qtsourceview/qate/tests] ./test1.clang 
Hello from Derived 1
Hello from MyClass
Hello from MyClass
Hello from MyClass

The question is

My original code was:

class Derived3: public MyClass, public IBase{
    virtual void IBase::printMsg(){ MyClass::printMsg(); }
};

Which is what I want to express, but this does not compile. I must admit I do not fully understand why this code work, as I expect that the new method Derived3::printMsg() will be an implementation of MyClass::printMsg() and not IBase::printMsg() (even tough this is what I do want).

How does the compiler chooses which method to re-implement, when two “sister classes” have the same virtual function name?

If anyone has a better way of implementing this, I would like to know as well 🙂

  • 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-15T07:02:47+00:00Added an answer on May 15, 2026 at 7:02 am

    The answer is, compiler overrides both functions, as can be shown by this sample:

    #include <cstdio>
    using std::printf;
    
    class A {
    public:
        virtual void a() {
            printf("A::a\n");
        }
    };
    class B {
    public:
        virtual void a() {
            printf("B::a\n");
        }
    };
    class C : public A, public B {
    public:
        virtual void a() {
            printf("C::a\n");
            A::a();
            B::a();
        }
    };
    int main() {
        C c;
        A &a = c;
        B &b = c;
        printf("Calling C::a via A\n");
        a.a();
        printf("Calling C::a via B\n");
        b.a();
    }
    

    The output is:

    Calling C::a via A
    C::a
    A::a
    B::a
    Calling C::a via B
    C::a
    A::a
    B::a
    

    If you want to override one and not the other, you need to rename it. Not only it will do what you want, but it will be clearer as well.

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

Sidebar

Related Questions

I've got this piece of legacy code which I'm trying to refactor at the
I'm trying to refactor some legacy code to use Spring to handle the jms
I'm trying to refactor this code. I have this jquery ajax call $.ajax({ url:
I am trying to Refactor this code as it is repeated ad nauseum throughout
I'm trying to refactor my sinatra code to separate my main file into separate
trying to refactor code to provide clean association A GAME has a HOME_TEAM and
I am trying to refactor some delphi 7 code. One of the procedures I'd
I am trying to refactor a half finished project. Original developer has left. In
I have the following code for adding to/extracting from Zip. I'm trying to refactor
I am trying to refactor some code while leaving existing functionality in tact. I'm

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.