将多个功能组合到单个通用功能

时间:2016-09-22 14:32:21

标签: c++ code-readability

我有几个函数来初始化一个接口指针,每个函数初始化特定版本的指针。现在我必须将这些功能集成到一个通用函数中。

private DaggerTestClass daggerTestClass; // note: no @Inject here

// …

// Inject other dependencies into the activity
IndexApplication.getApplication().getAppComponent().inject(this);
// "manually inject" the DaggerTestClass
this.daggerTestClass = IndexApplication.getApplication().getAppComponent()
    .daggerTestFactory().create("arg1", 2, true);

我想知道下面的原型是否有更好的实现,因为每个语句都需要显式转换接口指针,而且Visual Studio Intellisense也很难获取细节。

bool init_9(MSTSCLib::IMsRdpClient9* iface)
{
    iface->putSomeData1();
    iface->putSomeData2();
    iface->putSomeData3();
} 

bool init_8(MSTSCLib::IMsRdpClient8* iface)
{
    iface->putSomeData2();
}

bool init_7(MSTSCLib::IMsRdpClient7* iface)
{
    iface->putSomeData1();
    iface->putSomeData3();
}

1 个答案:

答案 0 :(得分:1)

好像你只是在寻找功能重载:

bool init(MSTSCLib::IMsRdpClient9*);
bool init(MSTSCLib::IMsRdpClient8*);
bool init(MSTSCLib::IMsRdpClient7*);

bool init(MSTSCLib::IMsRdpClient9* iface)
{
    iface->putSomeData1();
    iface->putSomeData2();
    iface->putSomeData3();
} 

// ...