接口与继承层次结构中的实现分离(C ++新手) [英] Separation of interface from implementation in an inheritance hierarchy (C++ newbie)

查看:42
本文介绍了接口与继承层次结构中的实现分离(C ++新手)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何安排一些课程.这就是我到目前为止所得到的...

I am trying to figure out how to arrange some classes. This is what I've got so far ...

(自然地)继承层次结构的顶部是T:

The top of the inheritance hierarchy is (naturally) T:

(T.h)
namespace foo
{
    class T
    {
    public:
        virtual void method1(std::string a_parameter) = 0;
        virtual void method2() = 0;
    };
}

我有两个T的子类以及一些其他方法-这是头文件:

I have two sub-classes of T with some additional methods - here are the header files:

(A.h)
namespace foo
{
    class A : public T
    {
    public:
        virtual ~A() {};
        virtual void method3() = 0;
        //and a factory function
        static A* gimmeAnAyy();
    };
}

(B.h)
namespace foo
{
    class B : public T
    {
    public:
        virtual ~B() {};
        virtual void method4() = 0;
        //and a factory function
        static B* gimmeABee();
    };
}

实现类在各自的.cpp文件中:

The implementation classes are in the respective .cpp files:

(A.cpp)
namespace foo
{
    class AImpl : public A
    {
    public:
        A(std::string member_data) : m_member_data(member_data) {};
        ~A() {};
        void method3()
        {
            //something really important, can't think of it right now ;-)
        };
    private:
        std::string m_member_data;
    };
    A* A::gimmeAnAyy()
    {
        return new AImpl("this is an impl of A");
    }; 
}

(B.cpp)
namespace foo
{
    class BImpl : public B
    {
    public:
        B(std::string other_data) : m_other_data(other_data) {};
        ~B() {};
        void method4()
        {
            //something really important, can't think of it right now ;-)
        };
    private:
        std::string m_other_data;
    };
    B* B::gimmeABee()
    {
        return new BImpl("this is an imll of B");
    }; 
}

现在,编译器抱怨-正确地-关于虚函数我尚未在AImpl和BImpl中实现的method1()和method2().

Now the compiler complains - rightly so - about the virtual functions method1() and method2() that I haven't implemented in AImpl and BImpl.

我想要的是AImpl和BImpl都可以继承的TImpl类这样我就不必在两个不同的.cpp文件中实现method1()和method2().

What I want is a TImpl class that both AImpl and BImpl can inherit from so that I don't have to implement method1() and method2() in two different .cpp files.

有可能吗?我要出去吃午饭吗?我是否对StackExchange帖子提出过多的反问?

Is it possible? Am I out to lunch? Am I asking too many rhetorical questions for a StackExchange post?

迈克

推荐答案

是的,有可能.常见的做法是使用以下代码段:

Yeah, it is possible. Common practice is to use the following snippet:

template<typename Interface>
class TImpl : public Interface
{
public:
    virtual void method1(std::string a_parameter) { /* implementation */ }
    virtual void method2() { /* implementation */ }
};

然后从其继承,如下所示:

And then inherit from it as follows:

class Aimpl : public TImpl<A>
{
public:
    virtual void method3() { /* implementation */ }
};

class Bimpl : public Timpl<B>
{
public:
    virtual void method4() { /* implementation */ }
};

您可以将Timpl的实现放入cpp文件中,但随后必须为每个可能的接口显式实例化它.在cpp中按以下步骤进行操作:

You can put implementation of Timpl in cpp file, but then you have to explicitly instantiate it for every possible interface. This is done as follows in the cpp:

template<typename Interface>
void Timpl<Interface>::method1(std::string a_parameter)
{
    /* implementation */
}

template<typename Interface>
void Timpl<Interface>::method2()
{
    /* implementation */
}

template class Timpl<A>;
template class Timpl<B>;

这篇关于接口与继承层次结构中的实现分离(C ++新手)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆