, , , , ( undefined) .
base::test derived::test , -, base::foo derived::bar. bar, derived, base, . derived base!!! undefined.
, , , this base derived , base. .
base::test virtual, : base, , base - , test .
derived::test ( base), ( vtable) .
, base::test , . :
class base {
public:
virtual bool test() const;
};
class derived : public base {
public:
virtual bool test() const;
};
int main()
{
derived d;
base & b = d;
if ( std::tr1::bind( &base::test, std::tr1::ref(b))() ) {
}
}
Note that there is no fill (castings are usually a hint of something strange, potentially dangerous hiding there), that the object is of a specific type, where you want this method to be called, and that the virtual sending mechanism ensures that even if bind matters base::testsince this method is virtual, the final forwarder will be executed.
This other example is likely to do funny things (I haven't tried it yet):
struct base {
void foo() {}
};
struct derived : base {
void foo() {
for ( int i = 0; i < 1000; ++i ) {
std::cout << data[i];
}
}
int data[1000];
};
int main() {
base b;
std::tr1::bind((void (base::*)()) &derived::foo, std::tr1::ref(b))();
}