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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:02:06+00:00 2026-06-02T01:02:06+00:00

Let’s say I have a C++ interface class Iface { public: virtual ~Iface ()

  • 0

Let’s say I have a C++ interface

class Iface
{
public:
    virtual ~Iface () {}
    virtual void foo () = 0;
    virtual void bar () = 0;
};

And I have some set of classes that needs to expose that on one side and implement it off somewhere else. I find that quite commonly I end up with a bunch of classes in the middle which essentially do something like this:

class InTheMiddleSomewhere
    : public Iface // amongst other things
{
public: // Iface
    virtual void foo () { if (impl) impl->foo (); }
    virtual void bar () { if (impl) impl->bar (); }
private:
    IfaceImpl* impl;
};

Which gets tiresome to write and maintain as the interfaces expand, proliferate and change.

Q: Is there a better (more or less automatic) way to implement a pass through for a complete interface than coding all that “if (impl) impl->…” stuff by hand?

  • 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-02T01:02:07+00:00Added an answer on June 2, 2026 at 1:02 am

    You can bind the call into a function object, and then just pass through the function object (rather than making a delegate call for each member function of the interface).

    Consider the following:

    struct I
    {
        virtual R1 f1(A1, B1, ..., Z1) = 0;
        virtual R2 f2(A2, B2, ..., Z2) = 0;
        .
        .
        virtual Rn fn(An, Bn, ..., Zn) = 0;
    };
    

    Now I have some implementation of I:

    struct : I
    {
        ...
    } x, y, z;
    

    So I can call x, y and zs function like:

    Ri xri = x.fi(ai, bi, ..., zi);
    Ri yri = y.fi(ai, bi, ..., zi);
    Ri zri = z.fi(ai, bi, ..., zi);
    

    or I can bind a call to a method of the I interface as follows:

    function<Ri(I&)> g = bind(&I::fi, _1, ai, bi, ..., zi);
    

    and then later:

    Ri xri = g(x);
    Ri yri = g(y);
    Ri zri = g(z);
    

    This works even if x, y and z are different classes, as long as they are derived from I.

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

Sidebar

Related Questions

let's say I have a class that does some calculations. This set of calculations
Let's say I have the string: hello world; some random text; foo; How could
Let's say we have the following: abstract class A; class B : public A;
Let's say we have: @Entity public class Order { @Id private int id; @OneToMany(mappedBy=order)
Let's say we have 3 classes: A, B and C. Each class has the
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say there is a graph and some set of functions like: create-node ::
Let's say I have one class User, and it has a property of type
Let's say I have two Entity classes: SocialApp and SocialAppType In SocialApp I have
Let's say you have a class library project that has any number of supplemental

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.