如何在C ++中实现非虚拟接口模式?

时间:2009-08-13 17:48:47

标签: c++ design-patterns

我对两者都很好奇,一个单线程和多线程实现。

由于

4 个答案:

答案 0 :(得分:3)

你的问题很模糊,但听起来你想要Curiously recurring template pattern

有很多比我好的人在网上解释这个,它在boost库中被大量使用。查看boost.iterator文档和代码以获得一个好例子

答案 1 :(得分:2)

Here you go ...

谷歌,这不是很棒吗? :P

答案 2 :(得分:2)

如果您有一份 Effective C ++ (第3版),Scott Meyers在第35项(第170页)中对NVI习语进行了很好的处理。

答案 3 :(得分:2)

class base
{
public:
    void bar()
    {
        getReady();
        barImpl();
        cleanup();
    }
    void getReady() {cout << "Getting ready. ";}
    void cleanup()  {cout << "Cleaning up.\n";}
protected:
    virtual void barImpl() {cout << "base::barImpl. ";}
}

class derived : public base
{
protected:
    virtual void barImpl() {cout << "derived::barImpl. ";}
}

int main()
{
    base b;
    derived d;
    b.bar();
    d.bar();
}

输出:

Getting ready. base::barImpl. Cleaning up.
Getting ready. derived::barImpl. Cleaning up.