Re: Run-time-checking whether a pure virtual function has been implemented
James Kanze wrote:
On Aug 27, 8:05 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
mzdude wrote:
On Aug 27, 9:04 am, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
Nordl?w wrote:
Is it possible to do runtime-checks whether a virtual
base-class pure virtual function has been implemented in
an inherited class?
Run-time? Check? Why? Besides, what would be the point?
If you have the object, it can't be of the abstract base
type, can it? An object cannot be instantiated with the
type that is abstract. If you have the pointer to the base
class, the virtual function table (the usual way to
implement the mechanism) contains the pointer to the final
overrider for that function, not the unimplemented pure
0...
What problem are you trying to solve?
I take to mean
#include <iostream>
using namespace std;
class Foo {
public:
virtual void bar() = 0 { cout << "Hello "; }
};
class Bar : public Foo
{
public:
virtual void bar() { cout << "World\n"; }
};
int main()
{
Bar b;
// Some kind of test to see if
// Foo::bar is implemented
You mean there is a definition of that pure virtual function,
yes?
I'm not too sure what he is asking, but I don't think it has
anything to do with this---he did say "implemented in an
inherited class". (Of course, taken literally, the answer is
simple: there's no runtime check, because if it isn't
implemented in a derived class, you can't instantiate objects of
that type.)
But you can, as subobjects of the some other derived class.
Theoretically, I still might want to check if the "child" has
implemented some pure virtual function or not, which would mean that the
function is defined in some "[great-]grandchild".
b.Foo::bar();
b.bar();
return 0;
}
Code that calls a pure virtual function has undefined
behavior.
No. Code that would resolve dynamically to a pure virtual
function is undefined behavior, but code that doesn't use
dynamic resolution can use it.
Is that what you're trying to avoid?
There is no portable way to determine that a pure virtual
function has been implemented, AFAICT.
It has been implemented. Otherwise, you can't instantiate
objects of the type. If the inheritance hierarchy is more than
two levels, however, there's no way to know whether it was
instantiated in the most derived class, or in one of the
intermediate classes. (Maybe that's what he's asking. Maybe
not.)
Exactly.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask