指向虚拟成员函数的指针

时间:2015-05-12 16:20:58

标签: c++ pointers

考虑以下代码

DERIVED a;
(a.*p_base)();

p_base是指向类成员函数的指针,但它是多态的。 这意味着

{{1}}

将打印DERIVED :: test_f

我怎样才能获得指向基类test_f的指针来进行非多态调用?

1 个答案:

答案 0 :(得分:1)

使用std::mem_funhttp://en.cppreference.com/w/cpp/utility/functional/mem_fn

示例:

DERIVED a;
f = mem_fun(&BASE::test_f);
f(a); // calls BASE's test_f with a pointer to a as the "this" pointer
相关问题