PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Check if virtual function has been overloaded
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Check if virtual function has been overloaded

Réponse
 
LinkBack Outils de la discussion
Vieux 28/12/2007, 01h53   #1
Mark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Check if virtual function has been overloaded

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
}
}
  Réponse avec citation
Vieux 28/12/2007, 03h44   #2
Salt_Peter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Check if virtual function has been overloaded

On Dec 27, 8:53 pm, Mark <mnbaya...@gmail.com> wrote:
> 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 { }
};






  Réponse avec citation
Vieux 28/12/2007, 05h50   #3
Dave Rahardja
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Check if virtual function has been overloaded

On 2007-12-27 19:53:48 -0600, Mark <mnbayazit@gmail.com> said:

> 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

  Réponse avec citation
Vieux 28/12/2007, 06h03   #4
Mark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Check if virtual function has been overloaded

On Dec 27, 7:44 pm, Salt_Peter <pj_h...@yahoo.com> wrote:
> On Dec 27, 8:53 pm, Mark <mnbaya...@gmail.com> wrote:
>
> > 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 { }
>
> };


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.
  Réponse avec citation
Vieux 28/12/2007, 06h12   #5
Mark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Check if virtual function has been overloaded

On Dec 27, 9:50 pm, Dave Rahardja
<drahardja.place...@sign.here.pobox.com> wrote:
> On 2007-12-27 19:53:48 -0600, Mark <mnbaya...@gmail.com> said:
>
> > 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


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.
  Réponse avec citation
Vieux 28/12/2007, 08h33   #6
Salt_Peter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Check if virtual function has been overloaded

On Dec 28, 1:03 am, Mark <mnbaya...@gmail.com> wrote:
> On Dec 27, 7:44 pm, Salt_Peter <pj_h...@yahoo.com> wrote:
>
>
>
> > On Dec 27, 8:53 pm, Mark <mnbaya...@gmail.com> wrote:

>
> > > 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 { }

>
> > };

>
> 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()
*/





  Réponse avec citation
Vieux 28/12/2007, 13h36   #7
Pavel Shved
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Check if virtual function has been overloaded

On 28 ÄÅË, 04:53, Mark <mnbaya...@gmail.com> wrote:
>
> 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.
  Réponse avec citation
Vieux 29/12/2007, 04h22   #8
Dave Rahardja
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Check if virtual function has been overloaded

On 2007-12-28 00:12:40 -0600, Mark <mnbayazit@gmail.com> said:

> On Dec 27, 9:50 pm, Dave Rahardja
> <drahardja.place...@sign.here.pobox.com> wrote:
>> On 2007-12-27 19:53:48 -0600, Mark <mnbaya...@gmail.com> said:
>>
>>> 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

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

  Réponse avec citation
Vieux 29/12/2007, 23h20   #9
Mark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Check if virtual function has been overloaded

On Dec 28, 8:22 pm, Dave Rahardja
<drahardja.place...@sign.here.pobox.com> wrote:
> On 2007-12-28 00:12:40 -0600, Mark <mnbaya...@gmail.com> said:
>
>
>
> > On Dec 27, 9:50 pm, Dave Rahardja
> > <drahardja.place...@sign.here.pobox.com> wrote:
> >> On 2007-12-27 19:53:48 -0600, Mark <mnbaya...@gmail.com> said:

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

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


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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h17.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20707 seconds with 17 queries