Is it possible to call virtual method in derived class from baseclass without declaring it abstract?

Discussion in 'C++' started by Jaco Naude, Sep 28, 2010.

  1. Jaco Naude

    Jaco Naude Guest

    Hi

    Not sure if this is a stupid question, but here it goes:

    Is it possible to call a virtual method in the derived class from a
    base class? I know that the method can be made abstract but then the
    whole class also becomes and abstract base class.

    class BaseClass {
    virtual bool method(bool call_derived) {
    if (call_derived)
    // I want to call method() in DerivedClass without knowing
    anything about it.
    else
    return false;
    }
    }

    class DerivedClass : public BaseClass {
    bool method() { return true; }
    }

    Thus when I do this in my main function:
    BaseClass base;
    base.method(true); // Should return true;
    base.method(false); // Should return false;

    Is this even possible at all? Or is the only way to do this by
    declaring method() = 0 in the base class (like a normal interface
    class).

    Thanks,
    Jaco
    Jaco Naude, Sep 28, 2010
    #1
    1. Advertising

  2. * Jaco Naude, on 28.09.2010 11:07:
    >
    > Not sure if this is a stupid question, but here it goes:
    >
    > Is it possible to call a virtual method in the derived class from a
    > base class? I know that the method can be made abstract but then the
    > whole class also becomes and abstract base class.
    >
    > class BaseClass {
    > virtual bool method(bool call_derived) {
    > if (call_derived)
    > // I want to call method() in DerivedClass without knowing
    > anything about it.
    > else
    > return false;
    > }
    > }
    >
    > class DerivedClass : public BaseClass {
    > bool method() { return true; }
    > }
    >
    > Thus when I do this in my main function:
    > BaseClass base;
    > base.method(true); // Should return true;
    > base.method(false); // Should return false;
    >
    > Is this even possible at all? Or is the only way to do this by
    > declaring method() = 0 in the base class (like a normal interface
    > class).


    It seems you have some misconception about virtual member functions, but I'm not
    sure exactly what it is.

    It would have been easier if you gave some complete example and explained what
    you expected and why, and what behavior you instead actually got; see the FAQ
    item about how to post a question about Code That Does Not Work.

    Anyway, does this code do what you're after?

    class Base
    {
    protected:
    virtual bool virtualBlah() const { return false; }
    public:
    bool blah( bool something ) const
    {
    return (something? virtualBlah() : false);
    }
    };

    class Derived: public Base
    {
    protected:
    virtual bool virtualBlah() const { return true; }
    };


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Sep 28, 2010
    #2
    1. Advertising

  3. Jaco Naude

    cpp4ever Guest

    On 09/28/2010 10:07 AM, Jaco Naude wrote:
    > Hi
    >
    > Not sure if this is a stupid question, but here it goes:
    >
    > Is it possible to call a virtual method in the derived class from a
    > base class? I know that the method can be made abstract but then the
    > whole class also becomes and abstract base class.
    >
    > class BaseClass {
    > virtual bool method(bool call_derived) {
    > if (call_derived)
    > // I want to call method() in DerivedClass without knowing
    > anything about it.
    > else
    > return false;
    > }
    > }
    >
    > class DerivedClass : public BaseClass {
    > bool method() { return true; }
    > }
    >
    > Thus when I do this in my main function:
    > BaseClass base;
    > base.method(true); // Should return true;
    > base.method(false); // Should return false;
    >
    > Is this even possible at all? Or is the only way to do this by
    > declaring method() = 0 in the base class (like a normal interface
    > class).
    >
    > Thanks,
    > Jaco


    If you wish to ensure that the base class member function is called you
    need to call the member function using a fully qualified name

    Hence in DerivedClass you would need to call as follows

    BaseClass::method(); // call BaseClass function
    method(); // calls DerivedClass function

    This will work even if it is called via a BaseClass pointer to a
    DerivedClass object.

    HTH

    cpp4ever
    cpp4ever, Sep 28, 2010
    #3
  4. Re: Is it possible to call virtual method in derived class from base class without declaring it abstract?

    Jaco Naude <> wrote:
    > Is it possible to call a virtual method in the derived class from a
    > base class? I know that the method can be made abstract but then the
    > whole class also becomes and abstract base class.
    >
    > class BaseClass {
    > virtual bool method(bool call_derived) {
    > if (call_derived)
    > // I want to call method() in DerivedClass without knowing
    > anything about it.
    > else
    > return false;
    > }
    > }


    I don't really understand what you are trying to do, but what you say
    sounds a lot like what you want is this:

    class BaseClass
    {
    public:
    bool method(bool call_derived)
    {
    if(call_derived)
    return methodImplementation();
    else
    return BaseClass::methodImplementation();
    }

    protected:
    virtual bool methodImplementation() { return false; }
    };

    class Derived: public BaseClass
    {
    protected:
    virtual bool mehtodImplementation() { return true; }
    };
    Juha Nieminen, Sep 28, 2010
    #4
  5. On 28 Sep., Jaco Naude wrote:
    > Hi
    >
    > Not sure if this is a stupid question, but here it goes:
    >
    > Is it possible to call a virtual method in the derived class from a
    > base class? I know that the method can be made abstract but then the
    > whole class also becomes and abstract base class.
    >
    > class BaseClass {
    >     virtual bool method(bool call_derived) {
    >         if (call_derived)
    >             // I want to call method() in DerivedClass without knowing
    > anything about it.
    >         else
    >             return false;
    >     }
    >
    > }
    >
    > class DerivedClass : public BaseClass {
    >     bool method() { return true; }
    >
    > }
    >
    > Thus when I do this in my main function:
    > BaseClass base;
    > base.method(true); // Should return true;
    > base.method(false); // Should return false;
    >
    > Is this even possible at all? Or is the only way to do this by
    > declaring method() = 0 in the base class (like a normal interface
    > class).
    >
    > Thanks,
    > Jaco


    The following should do the trick:

    #include <iostream>
    #include <assert.h>

    class BaseClass
    {

    public:
    virtual bool method(bool call_derived)
    {
    if (call_derived)
    {
    // I want to call method() in DerivedClass
    // without knowing anything about it.
    return method (call_derived);
    }
    else
    return false;
    }

    };

    class DerivedClass : public BaseClass
    {
    bool method (bool call_derived)
    {
    return true;
    }
    };

    int main ()
    {
    DerivedClass d;
    BaseClass& base = d;
    std::cout << base.BaseClass::method(true) << std::endl; // Should
    return true;
    std::cout << base.BaseClass::method(false) << std::endl; // Should
    return false;
    }

    Regards,
    Stuart
    Stuart Redmann, Sep 28, 2010
    #5
  6. Jaco Naude

    Jaco Naude Guest

    On Sep 28, 9:49 am, cpp4ever <> wrote:
    > On 09/28/2010 10:07 AM, Jaco Naude wrote:
    >
    >
    >
    >
    >
    > > Hi

    >
    > > Not sure if this is a stupid question, but here it goes:

    >
    > > Is it possible to call a virtual method in the derived class from a
    > > base class? I know that the method can be made abstract but then the
    > > whole class also becomes and abstract base class.

    >
    > > class BaseClass {
    > >     virtual bool method(bool call_derived) {
    > >         if (call_derived)
    > >             // I want to call method() in DerivedClass without knowing
    > > anything about it.
    > >         else
    > >             return false;
    > >     }
    > > }

    >
    > > class DerivedClass : public BaseClass {
    > >     bool method() { return true; }
    > > }

    >
    > > Thus when I do this in my main function:
    > > BaseClass base;
    > > base.method(true); // Should return true;
    > > base.method(false); // Should return false;

    >
    > > Is this even possible at all? Or is the only way to do this by
    > > declaring method() = 0 in the base class (like a normal interface
    > > class).

    >
    > > Thanks,
    > > Jaco

    >
    > If you wish to ensure that the base class member function is called you
    > need to call the member function using a fully qualified name
    >
    > Hence in DerivedClass you would need to call as follows
    >
    > BaseClass::method(); // call BaseClass function
    > method(); // calls DerivedClass function
    >
    > This will work even if it is called via a BaseClass pointer to a
    > DerivedClass object.
    >
    > HTH
    >
    > cpp4ever


    Thanks for the answers. The problem is not a piece of code that does
    not work, I'm trying to figure out if how something works in C++. I
    created a proper small example which answers my question:

    #include <QtCore/QCoreApplication>
    #include <stdio.h>

    class Base
    {
    protected:
    virtual bool virtualBlah() const {
    return false;
    }
    public:
    bool blah() const {
    return virtualBlah();
    }
    };

    class Derived: public Base
    {
    protected:
    virtual bool virtualBlah() const {
    return true;
    }
    };

    int main(int argc, char *argv[])
    {
    Base* base = new Base();
    Derived* derived = new Derived();

    if (base->blah())
    printf("Call on base class instance: True\n");
    else
    printf("Call on base class instance: False\n");

    Base* derived_base_ptr = reinterpret_cast<Base*> (derived);
    if (derived_base_ptr->blah())
    printf("Call on derived class instance: True\n");
    else
    printf("Call on derived class instance: True\n");

    return a.exec();
    }

    The output of the program is:
    Call on base class instance: False
    Call on derived class instance: True

    I always thought that if you call blah() on derived_base_ptr it will
    call the base class implementation. To get to the derived class
    implementation I thought you need a Derived*, unless you make the base
    class an abstract base class.

    Thank you for your help, it is appreciated.
    Jaco
    Jaco Naude, Sep 28, 2010
    #6
  7. Jaco Naude

    Jaco Naude Guest

    On Sep 28, 12:01 pm, Jaco Naude <> wrote:
    > On Sep 28, 9:49 am, cpp4ever <> wrote:
    >
    >
    >
    >
    >
    > > On 09/28/2010 10:07 AM, Jaco Naude wrote:

    >
    > > > Hi

    >
    > > > Not sure if this is a stupid question, but here it goes:

    >
    > > > Is it possible to call a virtual method in the derived class from a
    > > > base class? I know that the method can be made abstract but then the
    > > > whole class also becomes and abstract base class.

    >
    > > > class BaseClass {
    > > >     virtual bool method(bool call_derived) {
    > > >         if (call_derived)
    > > >             // I want to call method() in DerivedClass without knowing
    > > > anything about it.
    > > >         else
    > > >             return false;
    > > >     }
    > > > }

    >
    > > > class DerivedClass : public BaseClass {
    > > >     bool method() { return true; }
    > > > }

    >
    > > > Thus when I do this in my main function:
    > > > BaseClass base;
    > > > base.method(true); // Should return true;
    > > > base.method(false); // Should return false;

    >
    > > > Is this even possible at all? Or is the only way to do this by
    > > > declaring method() = 0 in the base class (like a normal interface
    > > > class).

    >
    > > > Thanks,
    > > > Jaco

    >
    > > If you wish to ensure that the base class member function is called you
    > > need to call the member function using a fully qualified name

    >
    > > Hence in DerivedClass you would need to call as follows

    >
    > > BaseClass::method(); // call BaseClass function
    > > method(); // calls DerivedClass function

    >
    > > This will work even if it is called via a BaseClass pointer to a
    > > DerivedClass object.

    >
    > > HTH

    >
    > > cpp4ever

    >
    > Thanks for the answers. The problem is not a piece of code that does
    > not work, I'm trying to figure out if how something works in C++. I
    > created a proper small example which answers my question:
    >
    > #include <QtCore/QCoreApplication>
    > #include <stdio.h>
    >
    > class Base
    > {
    > protected:
    >     virtual bool virtualBlah() const {
    >         return false;
    >     }
    > public:
    >     bool blah() const {
    >         return virtualBlah();
    >     }
    >
    > };
    >
    > class Derived: public Base
    > {
    > protected:
    >     virtual bool virtualBlah() const {
    >         return true;
    >     }
    >
    > };
    >
    > int main(int argc, char *argv[])
    > {
    >     Base* base = new Base();
    >     Derived* derived = new Derived();
    >
    >     if (base->blah())
    >         printf("Call on base class instance: True\n");
    >     else
    >         printf("Call on base class instance: False\n");
    >
    >     Base* derived_base_ptr = reinterpret_cast<Base*> (derived);
    >     if (derived_base_ptr->blah())
    >         printf("Call on derived class instance: True\n");
    >     else
    >         printf("Call on derived class instance: True\n");
    >
    >     return a.exec();
    >
    > }
    >
    > The output of the program is:
    > Call on base class instance: False
    > Call on derived class instance: True
    >
    > I always thought that if you call blah() on derived_base_ptr it will
    > call the base class implementation. To get to the derived class
    > implementation I thought you need a Derived*, unless you make the base
    > class an abstract base class.
    >
    > Thank you for your help, it is appreciated.
    > Jaco


    The was a typo in my previous post,

    Base* derived_base_ptr = reinterpret_cast<Base*> (derived);
    if (derived_base_ptr->blah())
    printf("Call on derived class instance: True\n");
    else
    printf("Call on derived class instance: True\n");

    should have been:

    Base* derived_base_ptr = reinterpret_cast<Base*> (derived);
    if (derived_base_ptr->blah())
    printf("Call on derived class instance: True\n");
    else
    printf("Call on derived class instance: False\n");

    I only saw all the other responses now. Thanks!
    I understand it now.
    Cheers
    Jaco
    Jaco Naude, Sep 28, 2010
    #7
  8. Jaco Naude

    Fred Zwarts Guest

    Re: Is it possible to call virtual method in derived class from base class without declaring it abstract?

    On Sep 28, 12:01 pm, Jaco Naude <> wrote:
    > On Sep 28, 9:49 am, cpp4ever <> wrote:
    >
    >
    >
    >
    >
    > > On 09/28/2010 10:07 AM, Jaco Naude wrote:

    >
    > > > Hi

    >
    > > > Not sure if this is a stupid question, but here it goes:

    >
    > > > Is it possible to call a virtual method in the derived class from a
    > > > base class? I know that the method can be made abstract but then the
    > > > whole class also becomes and abstract base class.

    >
    > > > class BaseClass {
    > > > virtual bool method(bool call_derived) {
    > > > if (call_derived)
    > > > // I want to call method() in DerivedClass without knowing
    > > > anything about it.
    > > > else
    > > > return false;
    > > > }
    > > > }

    >
    > > > class DerivedClass : public BaseClass {
    > > > bool method() { return true; }
    > > > }

    >
    > > > Thus when I do this in my main function:
    > > > BaseClass base;
    > > > base.method(true); // Should return true;
    > > > base.method(false); // Should return false;

    >
    > > > Is this even possible at all? Or is the only way to do this by
    > > > declaring method() = 0 in the base class (like a normal interface
    > > > class).

    >
    > > > Thanks,
    > > > Jaco

    >
    > > If you wish to ensure that the base class member function is called you
    > > need to call the member function using a fully qualified name

    >
    > > Hence in DerivedClass you would need to call as follows

    >
    > > BaseClass::method(); // call BaseClass function
    > > method(); // calls DerivedClass function

    >
    > > This will work even if it is called via a BaseClass pointer to a
    > > DerivedClass object.

    >
    > > HTH

    >
    > > cpp4ever

    >
    > Thanks for the answers. The problem is not a piece of code that does
    > not work, I'm trying to figure out if how something works in C++. I
    > created a proper small example which answers my question:
    >
    > #include <QtCore/QCoreApplication>
    > #include <stdio.h>
    >
    > class Base
    > {
    > protected:
    > virtual bool virtualBlah() const {
    > return false;
    > }
    > public:
    > bool blah() const {
    > return virtualBlah();
    > }
    >
    > };
    >
    > class Derived: public Base
    > {
    > protected:
    > virtual bool virtualBlah() const {
    > return true;
    > }
    >
    > };
    >
    > int main(int argc, char *argv[])
    > {
    > Base* base = new Base();
    > Derived* derived = new Derived();


    Note that you could also use

    Base* derived = new Derived();

    >
    > if (base->blah())
    > printf("Call on base class instance: True\n");
    > else
    > printf("Call on base class instance: False\n");
    >
    > Base* derived_base_ptr = reinterpret_cast<Base*> (derived);


    reinterpret_cast is dangerous and unnecessary when used in this way.

    Base* derived_base_ptr = derived;

    is easier and better.

    > if (derived_base_ptr->blah())
    > printf("Call on derived class instance: True\n");
    > else
    > printf("Call on derived class instance: True\n");
    >
    > return a.exec();
    >
    > }
    >
    > The output of the program is:
    > Call on base class instance: False
    > Call on derived class instance: True
    >
    > I always thought that if you call blah() on derived_base_ptr it will
    > call the base class implementation. To get to the derived class
    > implementation I thought you need a Derived*, unless you make the base
    > class an abstract base class.
    >
    > Thank you for your help, it is appreciated.
    > Jaco
    Fred Zwarts, Sep 28, 2010
    #8
  9. Jaco Naude

    Paul N Guest

    On 28 Sep, 13:01, Jaco Naude <> wrote:
    > I always thought that if you call blah() on derived_base_ptr it will
    > call the base class implementation. To get to the derived class
    > implementation I thought you need a Derived*, unless you make the base
    > class an abstract base class.


    If you call a function in the *constructor* of Base, then you will
    always get the base class version of the function. This is because,
    when the function is called, the object is only a Base - the rest of
    it hasn't been constructed yet.

    If you call a non-virtual function, then the version of the function
    you get depends on the type of the pointer. A pointer of type pointer-
    to-base gives you the base function, regardless of the actual type of
    the thing pointed to.

    If you call a virtual function (*not* in a constuctor), then the
    version of the function you get depends on the actual type of the
    thing being pointed to.

    Making the class abstract shouldn't make any difference, except that
    if you can't actually create a Base then it limits your options.

    Hope this helps.
    Paul.
    Paul N, Sep 28, 2010
    #9
    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. Sameer
    Replies:
    4
    Views:
    573
    Roedy Green
    Aug 31, 2005
  2. NewToCPP
    Replies:
    3
    Views:
    371
    NewToCPP
    Aug 30, 2006
  3. Chris Zopers

    Abstract method in non-abstract class

    Chris Zopers, Aug 8, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    358
    Göran Andersson
    Aug 8, 2008
  4. blangela
    Replies:
    8
    Views:
    648
    Erik Wikström
    Sep 26, 2008
  5. Stefan Ram
    Replies:
    27
    Views:
    716
    Stefan Ram
    Aug 17, 2009
Loading...

Share This Page