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

J

Jaco Naude

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
 
A

Alf P. Steinbach /Usenet

* 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
 
C

cpp4ever

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
 
J

Juha Nieminen

Jaco Naude said:
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; }
};
 
S

Stuart Redmann

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
 
J

Jaco Naude

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
 
J

Jaco Naude

On 09/28/2010 10:07 AM, Jaco Naude wrote:
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.

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
 
F

Fred Zwarts

On 09/28/2010 10:07 AM, Jaco Naude wrote:
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.

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.
 
P

Paul N

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top