Thread: How call derived version of pure virtual from base?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    How call derived version of pure virtual from base?

    How would I do this?

    Code:
    class Base
    {
    public:
         virtual string getName() const = 0;
    
         //I want this to call the implemented getName
         string getSomething()
         { return ( "Base." + getName() ); }
    };
    
    class Derived : public Base
    {
    public:
         virtual string getName() const
         { return "Derived"; }
    };
    Last edited by 6tr6tr; 04-16-2008 at 10:39 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In your case, it should simply work as when the derived class derives the functions, you can overload the getName() and it will call its own function (in Derived).
    (Which is basically already what you're doing.)

    But perhaps there is a more complex usage scenario that you're thinking of?
    Last edited by Elysia; 04-16-2008 at 10:38 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to make the member functions public. At the very least, getSomething() should be public and getName() protected.
    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

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Sorry, they are public in my own code, just forgot to write that, but when I try it it throws an error saying I tried to call a pure virtual function!

    Code:
    //This does NOT work
    Derived d();
    d.getSomething();

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    OK, I think I know what the problem is. Can this be "fixed"?

    The function getSomething() (which is defined as a base function) is called from the Base constructor. So I think that's why it tries to call the base version:

    Code:
    class Base
    {
    public:
         Base()
         { getSomething(); }
    }
    
    class Derived : public Base
    {
    public:
         Derived() : Base() {}
    };
    I have a bunch of functions that I want called in the Base constructor so every Derived class doesn't have to remember to call them, but I want those functions to call the derived versions. Is this possible?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This works for me:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Base
    {
    public:
         virtual string getName() const = 0;
    
         //I want this to call the implemented getName
         string getSomething()
         { return ( "Base." + getName() ); }
    };
    
    class Derived : public Base
    {
    public:
         virtual string getName() const
         { return "Derived"; }
    };
    
    int main()
    {
        Derived x;
        std::cout << x.getSomething() << std::endl;
    }
    Note that since Base is a polymorphic abstract base class, it should have a virtual destructor.

    Also,
    Code:
    Derived d();
    declare a function named d that has no parameters and returns a Derived object.

    The function getSomething() (which is defined as a base function) is called from the Base constructor.
    That is problematic. Virtual functions should never be called (directly or indirectly) from the constructor.
    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. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I have a bunch of functions that I want called in the Base constructor so every Derived class doesn't have to remember to call them, but I want those functions to call the derived versions. Is this possible?
    No, not without undefined behaviour, because the Base class constructs first and then the derived.
    A workaround might be a common init function in Base that's called in Derived's constructor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    No, not without undefined behaviour, because the Base class constructs first and then the derived.
    A workaround might be a common init function in Base that's called in Derived's constructor.
    I wouldn't call a virtual function from a constructor even at the same inheritance level. A better solution to this problem would be to use a factory pattern which constructs these objects and then, POST-construction, calls their init() methods.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I was more indicating though that the init & co should not be virtual.
    Otherwise a post-init solution may be required.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Thanks! I'll change the way I do it then.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Calling a pure virtual function from a constructor...
    By starcatcher in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2009, 09:12 AM
  3. call base class function or derived class function
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 05:23 AM
  4. concerning virtual functions
    By shadovv in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2005, 10:22 PM
  5. abstract class
    By xddxogm3 in forum C++ Programming
    Replies: 5
    Last Post: 01-01-2005, 09:08 AM