partial polymorphism

This is a discussion on partial polymorphism within the C++ Programming forums, part of the General Programming Boards category; If your design requires that your derived class must always call back to the base class, then you've implemented what ...

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    If your design requires that your derived class must always call back to the base class, then you've implemented what is known as the Call super antipattern

    If you follow the OP's description more closely, you'll see that the way he/she described the problem, it does sound more like it should be the base class calling a second pure-virtual method instead, which as it happens is the solution to the Call super antipattern.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    Quote Originally Posted by iMalc
    If you follow the OP's description more closely, you'll see that the way he/she described the problem, it does sound more like it should be the base class calling a second pure-virtual method instead, which as it happens is the solution to the Call super antipattern.
    That is why I suggested the non-virtual interface idiom (or if you prefer, the Template Method pattern).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by laserlight View Post
    That is why I suggested the non-virtual interface idiom (or if you prefer, the Template Method pattern).
    i practically found it to be much better

  4. #19
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by laserlight View Post
    That is why I suggested the non-virtual interface idiom (or if you prefer, the Template Method pattern).
    Oh you did too. Not sure how I missed that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #20
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by laserlight View Post
    Code:
    #include <iostream>
    
    class Base
    {
    public:
        void method()
        {
            std::cout << "base" << std::endl;
            vMethod();
        }
    
        ~Base() {}
    private:
        virtual void vMethod() {}
    };
    
    class Derived : public Base
    {
    private:
        virtual void vMethod()
        {
            std::cout << "derived" << std::endl;
        }
    };
    
    int main()
    {
        Derived d;
        Base& b = d;
        b.method();
    }
    ......................
    Note that the base class destructor is declared virtual.
    I agree it must be virtual
    but from the extract you proposed it seems you forgot to decalre it virtual?

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    Quote Originally Posted by mynickmynick
    but from the extract you proposed it seems you forgot to decalre it virtual?
    That's correct (or maybe it was merely a mix up in editing, but we'll never know). It was not my intention to leave out the virtual keyword.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by laserlight View Post
    That's correct (or maybe it was merely a mix up in editing, but we'll never know). It was not my intention to leave out the virtual keyword.
    thank you
    what are the things to care for / pay attention when a desctructor is virtual? I mean what are the most common errors a newbie might make in that case?
    Last edited by mynickmynick; 01-05-2009 at 10:22 AM. Reason: forgot to write thank you

  8. #23
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Don't do virtual function calls within a destructor?

    Don't forget to make the destructor virtual if the base class is to be used polymorphically?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #24
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    ok
    please give also other "must not do" examples you have experience of
    thank you

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism - "pointers" or "references"?
    By Petike in forum C++ Programming
    Replies: 10
    Last Post: 06-04-2009, 06:06 PM
  2. polymorphism with vars?
    By mynickmynick in forum C++ Programming
    Replies: 6
    Last Post: 01-07-2009, 03:53 AM
  3. Why do I get partial web-pages with recv?
    By hardi in forum Networking/Device Communication
    Replies: 11
    Last Post: 12-28-2006, 02:50 PM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. Partial Specialization error?
    By cboard_member in forum C++ Programming
    Replies: 10
    Last Post: 09-05-2005, 05:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21