|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 } } |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 { } }; |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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() */ |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|