wink <wi**@saville.comkirjutas:
I'd like to determine if a method has been overridden as was asked
here:
So, you need a dynamic dispatch based on the object type. The virtual
function system of C++ was invented to do just that, so it is a solution,
not a problem ;-)
If this solution does not match your needs, you can always fall back to C
style of implementing OOP, with more control over the implementation
details, see below. But such a need should not arise often, if ever, so
most probably whatever problem you have can be resolved in a better way.
hth
Paavo
#include <iostream>
class B;
class A;
void m_for_b(B* this_ptr) {
std::cout << "in m_for_b()\n";
}
void m_for_a(B* this_ptr) {
std::cout << "in m_for_a()\n";
}
class B {
public:
B():m_(&m_for_b) {}
~B() {}
typedef void (*m_t)(B*);
m_t m_;
};
class A : public B {
public:
A() {m_ = &m_for_a;}
~A() {}
};
void test(B &r) {
if (r.m_ == &m_for_b) {
// do something not overridden
std::cout << "not overridden" << std::endl;
} else {
// do something else was overridden
std::cout << "overridden" << std::endl;
}
(*r.m_)(&r);
}
int main() {
A a;
B b;
test(b);
test(a);
}
http://www.velocityreviews.com/forum...-whether-a-der
ived-class-overrides-a-virtual-memberfunction.html
The answer was can't do it, but I thought I'd ask here, my test code
is:
#include <iostream>
class B {
public:
B() {}
~B() {}
virtual void m() {}
};
class A : public B {
public:
A() {}
~A() {}
virtual void m() {}
};
void test(const B &r) {
if (&r.m == &B::m) {
// do something not overridden
std::cout << "not overridden" << std::endl;
} else {
// do something else was overridden
std::cout << "overridden" << std::endl;
}
}
int main(int argc, char *argv[]) {
A a;
B b;
test(b);
test(a);
return 0;
}
The compiler (gcc 4.0.3) complains:
tor.cc: In function 'void test(const B&)':
tor.cc:22: error: ISO C++ forbids taking the address of a bound member
function to form a pointer to member function. Say '&B::m'
Any suggestions on how this can be done?
Thanks
-- Wink Saville