Re: querying for virtual method existence
rabin shahav wrote:
Hi All,
I've a base class and about 500 derivatives classes. No, i'm not
confusing class instances with class definitions... and don't ask how
we end up having so many classes...
There are few methods which can be eliminated from the whole hierarchy
cone if at run time the base class could "know" if a certain method
was overridden.
Bottom line - i need the code for Foo::wasBarReDefinedNew
your help is well appreciated.
Rabin.
class Foo {
public:
virtual int bar(double, int, char ) ;
virtual bool wasBarReDefined()const=0;
bool wasBarReDefinedNew()const
{ // i need the code here...
return ????
}
};
class Bar1 : public Foo { // about 200 more Bar classes....
public:
virtual bool wasBarReDefined()const{return true;}
virtual int bar(double, int, char );
//....
};
class Baz1 : public Foo { // about 300 more Baz classes....
public: // no override of 'bar'
virtual bool wasBarReDefined()const{return false;}
///....
};
I am not aware of a general solution. Depending on how the
wasBarReDefined() client code really wants to use it, there may be some
particular solution though (I am guessing of course on scarce
information). For example, if the prospective client code of
wasBarReDefined() will always try to call bar() if it is redefined and
do other-thing otherwise, I would consider removing purity from Foo::bar
and renaming wasBarReDefined to tryCallingBar() because this is what it
is going to do then (also, it does not need to be virtual:
class Foo {
bool barIsNotRedefined_;
public:
bool tryCallingBar( // you do not need virtual here
double d, int i, char c, int *barResult) {
barIsNotRedefined_ = false;
*barResult = bar(d, i, c);
return !barIsNotRedefined_;
// returning true if some real bar was called.
}
virtual int bar (double, int, char)
{ barIsNotRedefined_ = true; }
};
// --------- client code
Foo *aFooDerivedThing;
// ... prepare arguments
if (!aFooDerivedThing->tryCallingBar(
aDouble, anInt, aChar, &anIntResult)) {
do-that-other-thing..
}
I wonder how close this is to what you really wanted to do though..
-Pavel
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]