Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何避免使用静态多态性重复代码_C++ - Fatal编程技术网

C++ 如何避免使用静态多态性重复代码

C++ 如何避免使用静态多态性重复代码,c++,C++,我有一个这样的接口(除了在真正的库代码中比这个长得多) 对于我来说,实现IFoo的不同监听器是很常见的。因此,我设计了一个助手类,如下所示: template <class T> struct IFooHelper { virtual void onA(A& a) { static_cast<T*>(this)->onGeneric(a); } virtual void onB(B& b) { static_cast<T*>

我有一个这样的接口(除了在真正的库代码中比这个长得多)

对于我来说,实现
IFoo
的不同监听器是很常见的。因此,我设计了一个助手类,如下所示:

template <class T>
struct IFooHelper {
    virtual void onA(A& a) { static_cast<T*>(this)->onGeneric(a); }
    virtual void onB(B& b) { static_cast<T*>(this)->onGeneric(b); }
    virtual void onC(C& c) { static_cast<T*>(this)->onGeneric(c); }
};
这已经非常好地工作了,但是现在我正在实现一个侦听器,我想要一些常见的行为,然后更新一个计数器,比如说,它是哪种类型的调用。换句话说,假设我只有上述类型
A、B、C
,我的监听器将是:

struct Ugly : public IFooHelper<Ugly>
{
    void onA(A& a) { //8 lines of common code; //update some counter for type A objs; }
    void onB(B& b) { //8 lines of common code; //update some counter for type B objs; }
    void onC(C& c) { //8 lines of common code; //update some counter for type C objs; }
};
struct丑陋:公共IFooHelper
{
void onA(A&A){//8行公共代码;//更新类型A objs的某些计数器;}
void onB(B&B){//8行公共代码;//更新类型B objs的某些计数器;}
void onC(C&C){//8行公共代码;//为类型C objs更新一些计数器;}
};

在这里,调用必须非常快(因此没有查找),理想情况下,我可以利用
IFooHelper
将常见行为提升到模板方法中,然后以某种方式仍然能够区分类型。我在想一种类似于模板专用结构的东西,它将偏移量转换为静态cons char*数组..或者根据
T
,使用char*值本身。有更好的方法吗?

我不确定是否完全理解您在寻找什么,但我会试一试。作为第一步,考虑这一点:

struct NotSoUgly : public IFooHelper<NotSoUgly>
{
    void updateCounter(A& a) { //update some counter for type A objs; }
    void updateCounter(B& b) { //update some counter for type B objs; }
    void updateCounter(C& c) { //update some counter for type C objs; }

    template <class T> void onGeneric(T& t) {
        //8 lines of common code;
        updateCounter(t);
    }
};
struct NotSoUgly:public IFooHelper
{
void updateCounter(A&A){//更新类型A objs的某些计数器;}
void updateCounter(B&B){//更新类型B objs的某些计数器;}
void updateCounter(C&C){//update some counter for type C objs;}
通用模板(T&T){
//8行通用代码;
更新计数器(t);
}
};

如果您向我们展示
updateCounter()
方法的内容,我们可能会有进一步的改进。我们也可以为此设计一个通用的实现,但如果没有看到代码,很难猜测。

希望有所帮助。对公共代码使用一个单独的方法如何,该方法将内联到其他方法中?@wich,想一想……目前我已经用一个模板专用结构宏实现了它,该宏带有数组中的静态偏移量。@PalaceChan在第二段代码中,这些函数应该没有不同的名称吗?@DavidRodríguez dribeas,谢谢你指出……是的,这是一个错误修复。谢谢,鉴于目前的情况,这似乎是最好的办法。我将在几个小时内进行编辑,以显示我最终做了什么。
struct Ugly : public IFooHelper<Ugly>
{
    void onA(A& a) { //8 lines of common code; //update some counter for type A objs; }
    void onB(B& b) { //8 lines of common code; //update some counter for type B objs; }
    void onC(C& c) { //8 lines of common code; //update some counter for type C objs; }
};
struct NotSoUgly : public IFooHelper<NotSoUgly>
{
    void updateCounter(A& a) { //update some counter for type A objs; }
    void updateCounter(B& b) { //update some counter for type B objs; }
    void updateCounter(C& c) { //update some counter for type C objs; }

    template <class T> void onGeneric(T& t) {
        //8 lines of common code;
        updateCounter(t);
    }
};