0

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

Thanks

vehomzzz
  • 42,832
  • 72
  • 186
  • 216
  • I am curious to know what you are talking about - you need to give a lot more detail, prefereably with example code. –  Aug 13 '09 at 17:58
  • 1
    To add to Neil, I'm not sure what you mean by single- vs. multi-threaded implementations. NVI doesn't really have anything to do with threading, and so the difference between single/multithreaded NVI is not really any different than the difference between the single/multithreaded implementations of any class. – Nick Meyer Aug 13 '09 at 18:11
  • @Nick The guy seems to be indulging in a minor C++ spam-fest. I doubt he understands his own question. –  Aug 13 '09 at 18:15
  • Enigma, you asked a question a bit ago, got this as your answer, now you are asking how to implement it. This usually stems from not understanding quite what you're trying to solve in the first place. – GManNickG Aug 13 '09 at 18:21
  • sorry, it just occurred to me and quite forgot the reasons behind Non-Virtual Interface pattern.. I could have googled for it, but was interested in SO member's opinions.. – vehomzzz Aug 13 '09 at 18:23
  • two answers about it: http://stackoverflow.com/questions/397404/virtual-function-call-from-base-class/397409#397409 http://stackoverflow.com/questions/343605/how-do-you-validate-an-objects-internal-state/343628#343628 – Johannes Schaub - litb Aug 13 '09 at 18:31

4 Answers4

3

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
  • 10,798
  • 3
  • 37
  • 41
2

Here you go...

Google, isn't it amazing? :P

Thorarin
  • 47,289
  • 11
  • 75
  • 111
2

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
  • 39,212
  • 14
  • 67
  • 75
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();
}

Output:

Getting ready. base::barImpl. Cleaning up.
Getting ready. derived::barImpl. Cleaning up.
rlbond
  • 65,341
  • 56
  • 178
  • 228
  • It's always nice with a code sample, but some explanation would do (although I see what it does). – Cecil Has a Name Aug 13 '09 at 18:18
  • In you case virtual is protected. what about private? – vehomzzz Aug 13 '09 at 18:20
  • private is fine, but a lot of books don't recommend it for virtual functions because the semantics are a little confusing. – rlbond Aug 13 '09 at 18:42
  • 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 Aug 13 '09 at 21:59