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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:58:23+00:00 2026-05-26T20:58:23+00:00

Edit: Per some comments, by simple I mean a) less code, b) easy to

  • 0

Edit: Per some comments, by simple I mean a) less code, b) easy to maintain, and c) hard to get wrong.

Edit #2: Also, using containment instead of private inheritance is not objectionable if it does indeed simplify the implementation of InterfaceImpl.

Currently, the only way I know to do this is to have the implementer define the abstract method and delegate the call to the target base type’s method. Example:

#include <iostream>
#include <memory>

class Interface
{
public:
    virtual void method1() = 0;
    virtual void method2(int x) = 0;
};

class MethodOneImpl
{
 private:
    void method1(int x)
    { std::cout << "MethodOneImpl::method1() " << x << std::endl; }

 public:
    void method1() { method1(0); }
};

class MethodTwoImpl
{
 public:
    void myFunc(int x)
    { std::cout << "MethodTwoImpl::myFunc(x)" << x << std::endl; }
};

class InterfaceImpl : public Interface
                    , private MethodOneImpl
                    , private MethodTwoImpl
{
public:    
    virtual void method1() { MethodOneImpl::method1(); }
    virtual void method2(int x) { MethodTwoImpl::myFunc(x); }
};

int main()
{
    std::unique_ptr<Interface> inf;
    inf.reset(new InterfaceImpl);
    inf->method1();
    inf->method2(0);

    // This should be disallowed!
    // std::unique_ptr<MethodOneImpl> moi;
    // moi.reset(new InterfaceImpl);
}

At first, I thought that perhaps this might solve the problem:

class InterfaceImpl : public Interface
                    , private MethodOneImpl
                    , private MethodTwoImpl
{
public:    
    using MethodOneImpl::method1;
    // Obviously this wouldn't work as the method names don't match.
    //using MethodTwoImpl::??? 
};

The first using statement will make both MethodOneImpl::method1 methods be public, but it actually doesn’t fulfill the contract with Interface, and it modifies the accessibility of MethodOneImpl::method1(int). And obviously we couldn’t use this solution with method2 as the names don’t match up.

FWIW, I have what I think is a solution, but it is not part of the standard at all (in other words it won’t compile). I was thinking of making a proposal to the C++ committee; if anyone has any advice, I’d appreciate any comments below (but please dont’ submit the advice as an answer).

  • 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-26T20:58:24+00:00Added an answer on May 26, 2026 at 8:58 pm

    An other option (at least if using MS VC++) is to use virtual inheritance:

    struct MyInterface
    {
        virtual void Method1() = 0;
        virtual void Method2() = 0;
    };
    
    class Method1Impl : public virtual MyInterface
    {
        virtual void Method1() { _tprintf( _T("Method1\n") ); }
    };
    
    class Method2Impl : public virtual MyInterface
    {
        virtual void Method2() { _tprintf( _T("Method2\n") ); }
    };
    
    class InterfaceImpl : public virtual MyInterface,
                          private Method1Impl,
                          private Method2Impl
    {
    };
    
    void TestWeirdInterfaceImpl()
    {
        MyInterface*    pItf = new InterfaceImpl();
    
        pItf->Method1();
        pItf->Method2();
    }
    

    While this seems to work and satisfy what you are looking for (asside from C4250 warning that you will have to suppress with a #pragma), this wouldn’t be my approach. (I believe virtual inheritance is still not something that supported across all compilers, but I could be wrong).

    I would probably go with containment and once boilerplate code is identifier, wrap it into some kind of macro map (similar to maps in ATL or MFC) that would make it really, really difficult to ever screw it up.

    So this would be my macro approach:

    struct MyInterface
    {
        virtual float Method1( int x ) = 0;
        virtual int Method2( float a, float b ) = 0;
        virtual void Method3( const TCHAR* sz ) = 0;
    };
    
    class Method1Impl
    {
    public:
        float Method1( int x ) {
            _tprintf( _T("Method1: %d\n"), x ); return 5.0;
        }
    };
    
    class Method2and3Impl
    {
    public:
        int Method2( float a, float b ) {
            _tprintf( _T("Method2: %f, %f\n"), a, b ); return 666;
        }
    
        void Method3( const TCHAR* sz ) {
            _tprintf( _T("Method3: %s"), sz );
        }
    };
    
    
    #define DECLARE_METHOD0( MethodName, Obj, R )   \
        virtual R MethodName() { return Obj.MethodName(); }
    
    #define DECLARE_METHOD1( MethodName, Obj, R, A1 )   \
        virtual R MethodName( A1 a1 ) { return Obj.MethodName( a1 ); }
    
    #define DECLARE_METHOD2( MethodName, Obj, R, A1, A2 )   \
        virtual R MethodName( A1 a1, A2 a2 ) { return Obj.MethodName( a1, a2 ); }
    
    
    class InterfaceImpl : public MyInterface
    {
    public:
        DECLARE_METHOD1( Method1, m_method1Impl, float, int );
        DECLARE_METHOD2( Method2, m_method2and3Impl, int, float, float );
        DECLARE_METHOD1( Method3, m_method2and3Impl, void, const TCHAR* );
    
    private:
        Method1Impl         m_method1Impl;
        Method2and3Impl     m_method2and3Impl;
    };
    
    void TestWeirdInterfaceImpl()
    {
        MyInterface*    pItf = new InterfaceImpl();
    
        pItf->Method1( 86 );
        pItf->Method2( 42.0, 24.0 );
        pItf->Method3( _T("hi") );
    }
    

    Until C++ gods grace us with variadic macros, you’ll have to declare one for each number of parameters you have. Also if you used multiple inheritance, potentially you wouldn’t need the second “Obj” param, but as I’ve said before, I’d avoid multiple inheritance if there’s another solution, which in this case is one extra param.

    Yet a third option could be something that authors of Pragmatic Programmer seem to advocate a lot. If you have a ton of cookie cutter code that you don’t want to repeat because, as you pointed out, it introduces human error. Define your own language and write a code generator script (python, perl…) to auto-create the actual code. In this case you could almost point at an interface, and have the script write the text out for you. I haven’t tried doing this kind of thing myself, but lately have been wanting to use it somewhere just to see and evaluate the outcome.

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

Sidebar

Related Questions

Edit, as per these comments: Do you mean "Property" vs "Field"? public String S1;
EDIT : Per Slaks $j('#banner-content img').each(function() { var link = $j('#page-tabs li a.' +
EDIT: For the inner queries, there could be more than one match per inner
How do you edit a tab label, per tab, in GVim? You can do
EDIT What small things which are too easy to overlook do I need to
I have some doubts, i m developing a web application in GWT using MVP
I keep getting the following error when trying to parse some html using BeautifulSoup:
I have some code that does a lot of comparisons of 64-bit integers, however
I'm drawing some 2D lines in OpenGL (using the AGL API). I've turned on
I am using Backbone.js (version 0.5.3) and am having some trouble with a success

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.