views:

259

answers:

4

I am curious about both, a h single and multi-threaded implementation.

Thanks

+2  A: 

Here you go...

Google, isn't it amazing? :P

Thorarin
+2  A: 

If you have a copy of Effective C++ (3rd Edition) sitting around, Scott Meyers gives a nice treatment of the NVI idiom in Item 35 (page 170).

Nick Meyer
Yes, I have it. I will take a look. thx
vehomzzz
+1  A: 
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();
}

Output:

Getting ready. base::barImpl. Cleaning up.
Getting ready. derived::barImpl. Cleaning up.
rlbond
It's always nice with a code sample, but some explanation would do (although I see what it does).
Cecil Has a Name
In you case virtual is protected. what about private?
vehomzzz
private is fine, but a lot of books don't recommend it for virtual functions because the semantics are a little confusing.
rlbond
Actually the semantics are about as easy as it gets: Overriding virtual functions is completely orthogonal to access specification. `:^>` (OK, I agree, it _is_ confusing.)
sbi
+1  A: 

Your question is vague, but it sounds like you want the Curiously recurring template pattern

There are a lot better people than I to explain this on the web it is used a lot in the boost library. check out boost.iterator documentation and code for a good example

iain