C++ pure virtual method defined at run-time


Keywords:c++ 


Question: 

Not sure if the name is very telling but here goes.

I am interfacing to an API that requires a base class to be inherited and a lot of pure virtual methods to be defined.

For good programming practice, I want these methods to be defined in different (sub) classes.

Currently, I use a facade/wrapper (kind of both) class, that inherits the base class, instantiates the sub-classes, and calls the necessary methods of these instantiated classes:

#include <cstdio>

class Base
{
  public:
    virtual void reqImplementation( void ) = 0;
};

class APIImplementation
{
  private:

    Base * ptr_;

  public:
    APIImplementation( Base * ptr ) :
      ptr_( ptr )
    {
      ptr_->reqImplementation();
    }
};

class MyImplementation
{
  private:
    APIImplementation * api_;
  public:
    void reqImplementation( void )
    {
      printf("Hello World!\n");
    }

    MyImplementation( APIImplementation * api ) : api_( api ) {}
};

class MyFacade : public Base
{
  private:
    MyImplementation * my_impl_;
    APIImplementation * api_;

    void reqImplementation( void )
    {
      my_impl_->reqImplementation();
    }

  public:
    MyFacade( void )
    {
      api_ = new APIImplementation( this );
      my_impl_ = new MyImplementation( api_ );
    }
};

int main( void )
{
  MyFacade my_facade;
  return 0;
}

Is there any way to implement the pure virtual functions in the sub-classes instantiated within this facade/wrapper? Or, alternately, what would be good practice for something like this? I want something similar to this (I know it clearly doesn't work at the moment):

#include <cstdio>

class Base
{
  public:
    virtual void reqImplementation( void ) = 0;
};

class APIImplementation
{
  private:

    Base * ptr_;

  public:
    APIImplementation( Base * ptr ) :
      ptr_( ptr )
    {
      ptr_->reqImplementation();
    }
};

class MyImplementation : public Base
{
  private:
    APIImplementation * api_;
  public:
    void reqImplementation( void )
    {
      printf("Hello World!\n");
    }

    MyImplementation( APIImplementation * api ) : api_( api ) {}
};

class MyFacade : public Base
{
  private:
    MyImplementation * my_impl_;
    APIImplementation * api_;

  public:
    MyFacade( void )
    {
      api_ = new APIImplementation( this );
      my_impl_ = new MyImplementation( api_ );
    }
};

int main( void )
{
  MyFacade my_facade;
  return 0;
}

Note the API source is open, so I can change these pure virtual methods to anything else, however the code is quite exhaustive, so I'd rather small changes than significant ones.


1 Answer: 

If I got you right, you need a virtual inheritance. Here's an example

#include <iostream>
using namespace std;

// base class with lots of methods (two, actually)
struct Base {
    virtual void f() = 0;
    virtual void g() = 0;
};

// here's a class with implementation of f
struct ImplementationPart1 : public virtual Base {
    virtual void f() {
        cout << 4;
    }
};

// here's a class with implementation of g
struct ImplementationPart2 : public virtual Base {
    virtual void g() {
        cout << 2;
    }
};

// here's a class with all the implementations
struct Implementation : public ImplementationPart1, public ImplementationPart2 {};

int main() {
    // Implementation inherits from Base, yes
    Base *x = new Implementation();
    // everything works, as expected
    x->f();
    x->g();
}

Live example.