Thread: partial polymorphism

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,318
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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

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, 05: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, 04:23 AM