Virtual function design pattern

Discussion in 'C Programming' started by George2, Mar 19, 2008.

  1. George2

    George2

    Joined:
    Feb 12, 2008
    Messages:
    108
    Hello everyone,


    About the virtual function design pattern mentioned,

    http://www.parashift.com/c -faq-lite/strange-inheritance.html#faq-23.4

    I do not quite understand what does this statement mean "Assuming they're protected, they probably shouldn't be virtual: if the derived class doesn't like the behavior in one of them, it doesn't have to call that method." Any ideas?

    I quote the whole paragraph here,

    --------------------
    Suppose you have the exact opposite situation from the previous FAQ, where you have a method whose overall structure is different in each derived class, yet it has little pieces that are the same in most (if not all) derived classes. In this case you'd put the overall algorithm in a public virtual that's ultimately defined in the derived classes, and the little pieces of common code can be written once (to avoid code duplication) and stashed somewhere (anywhere!). A common place to stash the little pieces is in the protected part of the base class, but that's not necessary and it might not even be best. Just find a place to stash them and you'll be fine. Note that if you do stash them in the base class, you should normally make them protected, since normally they do things that public users don't need/want to do. Assuming they're protected, they probably shouldn't be virtual: if the derived class doesn't like the behavior in one of them, it doesn't have to call that method.
    --------------------


    thanks in advance,
    George
    George2, Mar 19, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. IK
    Replies:
    2
    Views:
    592
    hemraj
    Jul 23, 2004
  2. sunny
    Replies:
    1
    Views:
    449
    Salt_Peter
    Dec 7, 2006
  3. Pallav singh
    Replies:
    0
    Views:
    341
    Pallav singh
    Jan 22, 2012
  4. Pallav singh
    Replies:
    0
    Views:
    387
    Pallav singh
    Jan 22, 2012
  5. Pallav singh
    Replies:
    1
    Views:
    442
    Peter Remmers
    Jan 22, 2012
Loading...

Share This Page