Check if virtual function has been overloaded

M

Mark

Hi,

I have an abstract class which I want my other classes to inherit
from. In the constructor of the abstract class I want to check if
certain virtual functions have been overloaded or not. Is this
possible?

For example, let's say I have

class Base {
public:
virtual void display();
};

class Parent: public: Base {
public:
void display()
}

Then what I want to do is this:

Base::Base() {
if(Parent class has defined display) {
// do something
} else {
// do something else
}
}
 
S

Salt_Peter

Hi,

I have an abstract class which I want my other classes to inherit
from. In the constructor of the abstract class I want to check if
certain virtual functions have been overloaded or not. Is this
possible?

Its not an overload, its an override.
For example, let's say I have

class Base {
public:
virtual void display();

};

class Parent: public: Base {

class Parent : public Base
{
public:
void display()

} ;

Then what I want to do is this:

Base::Base() {
if(Parent class has defined display) {
// do something} else {

// do something else

}
}

The situation you describe suggests a design problem. If Base needs to
know what interface Parent has, then the variation in Base should
really be in Parent.
That or you need an overload of Base's constructor (not override).
Override means hide, overload means supply an alternate signature.

silly example:

class Base
{
bool has_override;
public:
Base() : has_override(false) { }
Base(bool b) : has_override(b) { do something(); }
virtual void display() const { }
private:
void do_something()
{
if(has_override)
// do this
else
// do that
}
};

class Parent : public Base
{
public:
Parent() : Base(true) { }
void display() const { }
};
 
D

Dave Rahardja

Hi,

I have an abstract class which I want my other classes to inherit
from. In the constructor of the abstract class I want to check if
certain virtual functions have been overloaded or not. Is this
possible?

Why do you want to do this?

-dr
 
M

Mark

Its not an overload, its an override.





class Parent : public Base
{









The situation you describe suggests a design problem. If Base needs to
know what interface Parent has, then the variation in Base should
really be in Parent.
That or you need an overload of Base's constructor (not override).
Override means hide, overload means supply an alternate signature.

silly example:

class Base
{
bool has_override;
public:
Base() : has_override(false) { }
Base(bool b) : has_override(b) { do something(); }
virtual void display() const { }
private:
void do_something()
{
if(has_override)
// do this
else
// do that
}

};

class Parent : public Base
{
public:
Parent() : Base(true) { }
void display() const { }

};

Yeah... I realized I meant "override" just after posting it, but it's
kind of impossible to edit.
So I'd have to use booleans then? I was hoping I wouldn't...because
then I need about 10, and it just gets messy. Oh well, thanks.
 
M

Mark

Why do you want to do this?

-dr

Well... if you're curious.

If the Parent class has defined the function, I want to use that
function. If the Parent class has NOT defined the function, I *don't*
want to use the Base class' function, instead, I want to re-route to
another function in a stack. If I'm going to re-route it, I want to
pop an item off the stack, but then I pop an item off the stack in the
destructor, and I don't want to pop it off twice... so... I need to
know whether or not it's already been popped off. Yes, I could use
booleans, but I was hoping there was a more elegant way. In fact... I
did find another way that could potentially stop some future problems
I didn't originally anticipate.

That problem didn't really make a heck of a lot of sense, but I'd have
to explain most of my project before it would make any sense at all.
 
S

Salt_Peter

Yeah... I realized I meant "override" just after posting it, but it's
kind of impossible to edit.
So I'd have to use booleans then? I was hoping I wouldn't...because
then I need about 10, and it just gets messy. Oh well, thanks.

You don't have to. The boolean is a solution akin to a concept_check.

1) You want Parent::display() to be called when its available
2) when its not, you don't want to process Base::display()
3) you'ld like an alternate function to be processed instead

I'ld like to point out that Base::display() has complete access to the
hierarchy's interface.
It can call any member function it has access to with respect to
override rules.
A Parent knows its a Parent, there is no need to tell it.

So lets see what happens with no override...

#include<iostream>

class Base
{
public:
Base() { }
virtual ~Base() { }
virtual void display()
{
do_something();
}
private:
virtual void do_something()
{
std::cout << "Base::do_something()\n";
}
};

class Parent : public Base
{
public:
Parent() { }
// void display() { }
void do_something()
{
std::cout << "Parent::do_something()\n";
}
};

int main()
{
Parent parent;
parent.display();
}

/*
Parent::do_something()
*/
 
P

Pavel Shved

Then what I want to do is this:

Base::Base() {
if(Parent class has defined display) {
// do something} else {

// do something else

}
}

A rude, ugly misconception. The class' constructor doesn't and must
not know that class is or even can ever be derived. At the stage of
constructing Base we do not know whether it is `in-charge' or is not.
This stuff is left for compilers and user - you - have to _create_
every class like a final one.

I've underlined `create' word because, in opposite, the same doesn't
hold for destroying. So you can provide a mecahnism of informing base
class whether he should `pop item from a stack' in destructor. Just
add a corresponding boolean being set in Parent's virtual destructor.
And that re-routing is done by default in Base's virtual function
`default' body.
 
D

Dave Rahardja

Well... if you're curious.

If the Parent class has defined the function, I want to use that
function. If the Parent class has NOT defined the function, I *don't*
want to use the Base class' function, instead, I want to re-route to
another function in a stack. If I'm going to re-route it, I want to
pop an item off the stack, but then I pop an item off the stack in the
destructor, and I don't want to pop it off twice... so... I need to
know whether or not it's already been popped off. Yes, I could use
booleans, but I was hoping there was a more elegant way. In fact... I
did find another way that could potentially stop some future problems
I didn't originally anticipate.

That problem didn't really make a heck of a lot of sense, but I'd have
to explain most of my project before it would make any sense at all.

You have a design problem here. The fact that Base needs to know if
Parent has overridden a virtual function is indicative of a
pathological dependency.

Try looking for a different way to segregate your algorithm. Your Base
class should be a complete class, independent of any potential derived
classes. I suspect that what you'd have to do is to create a different
(or additional) virtual functions, so that Parent can simply extend,
and not modify, Base's management of the pop/no-pop state.

-dr
 
M

Mark

You have a design problem here. The fact that Base needs to know if
Parent has overridden a virtual function is indicative of a
pathological dependency.

Try looking for a different way to segregate your algorithm. Your Base
class should be a complete class, independent of any potential derived
classes. I suspect that what you'd have to do is to create a different
(or additional) virtual functions, so that Parent can simply extend,
and not modify, Base's management of the pop/no-pop state.

-dr

I'm building a callback wrapper for OpenGL. I can't callback non-
static functions, so instead I've created a series of static functions
(in the Base class) which in turn call the member functions of the
parent class. In order to do this, I need to maintain a few stacks to
figure out which member function should be called. The most recently
created class gets control of the callbacks, unless it doesn't have a
corresponding callback, in which class the previously created class re-
assumes control. In my solution, instead of popping off the class
pointer if the member callback doesn't exist, I simply redirect to the
appropriate class, and then pop them all off at the end. Seems to be
working well,... for now.

Thanks for the tips everyone.
 

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

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top