【问题标题】:C++ pure virtual method defined at run-time在运行时定义的 C++ 纯虚方法
【发布时间】:2014-06-03 13:51:00
【问题描述】:

不确定这个名字是否很有说服力,但这里是。

我正在连接一个需要继承基类并定义大量纯虚方法的 API。

为了良好的编程习惯,我希望在不同的(子)类中定义这些方法。

目前,我使用一个外观/包装器(两者都有)类,它继承基类,实例化子类,并调用这些实例化类的必要方法:

#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;
}

有没有办法在这个外观/包装器中实例化的子类中实现纯虚函数?或者,对于这样的事情,什么是好的做法?我想要类似的东西(我知道它目前显然不起作用):

#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;
}

请注意,API 源是开放的,因此我可以将这些纯虚拟方法更改为其他任何方法,但是代码非常详尽,因此我宁愿进行小的更改而不是重大更改。

【问题讨论】:

  • MyFacad 的公共方法有哪些?它似乎扮演着 Adaptor、Resource Manager 的角色……虽然他们在 main 中初始化它,但它似乎也暴露了你的 API?
  • (普遍关注的是您获得此设置的方式没有明确的所有权。如果 APIImplementation 实际上采用 ptr 参数,我想您可以解决它,但这似乎是一个好方法鼓励内存泄漏和段错误)
  • 公开 API... 如果我对公开 API 的理解是正确的,即在这种情况下,允许 API 开始调用 MyFacade 的方法(与之交互)。不幸的是,APIImplementation 是我希望保持不变的类。
  • 您有多种选择,但它们都有一些权衡,因此您必须多说一些您不喜欢您的解决方案的地方。顺便说一句,如果您在 MyFacade 上定义 reqImplementation,第二个代码确实会编译:ideone.com/WRyHhc
  • 如果没有一个简单地将方法调用重定向到子类的巨大外观,它似乎可以做得更好。我知道重新编译,但是代码没有按我的意图执行(APIImplementation 不知道调用 MyImplementation 方法,而不是 MyFacade 方法)

标签: c++ class inheritance polymorphism


【解决方案1】:

如果我没看错,你需要一个虚拟继承。这是一个例子

#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.

【讨论】:

    猜你喜欢
    • 2016-06-23
    • 1970-01-01
    • 2011-09-08
    • 2011-06-24
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 2011-07-10
    相关资源
    最近更新 更多