Re: Determine if a function has been overridden
S. I. Becker wrote:
Is it possible to determine if a function has been overridden by an
object, when I have a pointer to that object as it's base class (which
is abstract)? The reason I want to do this is that I want to call
this function if it has been overridden, and not if it hasn't, e.g
This seems like an attempt to work around a bad design. Why in the
world would you intentionally design your system like that?
class CBase
{
public:
virtual long mightBeOverridden(int i) { return 0; } // not a pure
virtual function, since sub-classes should not have to define it
bool isFunctionOverRidden() { return false; }// This is what I want
to code
virtual int id()=0; // this class is an abstract class because of
this function - I don't think that this is relevant but more info is
good };
class CDerived1
{
// Does not override the function
int id() { return 1; }
};
class Derived2
{
long mightBeOverridden(int i) { return i; }
// Should not have to also override isAboveFunctionOverRidden()
int id() { return 2; }
};
class Derived3
{
long mightBeOverridden(int i) { return i * i; }
// Should not have to also override isAboveFunctionOverRidden()
int id() { return 3; }
};
int foo(CBase* pObj, int i, int j)
{
if(pObj)
{
if(pObj->isFunctionOverridden())
return mightBeOverridden(i);
You mean
return pObj->mightBeOverridden(i);
}
return j;
}
I googled on this and came up with nought, as did a search of this
list. I'm guessing that means it's impossible. I thought that I
might compare function pointers ( return mightBeOverridden !=
CBase::mightBeOverridden ) but that doesn't distinguish between the
two.
I don't think it's possible without some trick with dynamic_cast which
means that 'foo' has to know about derived classes, which in turn shows
that the design is bad, which suggests that you need to revisit the
design instead of patching it up like that.
The whole point of virtual functions is that they are supposed to be
used *without* any regard to whether the derived class has or hasn't
overridden them. If suddenly you have an urge to learn that, then
you probably shoudn't have those functions virtual in the first place.
If it's relevant, I'm using MS Visual C++ 7.0 (aka .NET 2002), due to
upgrade to 8.0 soon.
It's not.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask