1

Consider the following code

class BASE
{
  public:
            virtual void test_f(){std::cout<<"BASE::test_f\n";}
};

class DERIVED:public BASE
{
    public:
            virtual void test_f(){std::cout<<"DERIVED::test_f\n";}
};

void (BASE::*p_base)() = &BASE::test_f;

p_base is a pointer to a class member function but it is polymorphic. That means that

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

will print DERIVED::test_f

How could i get the pointer to a test_f of the base class to make NON polymorphic call?

Fedor
  • 17,146
  • 13
  • 40
  • 131
zulunation
  • 297
  • 1
  • 5
  • 13

1 Answers1

1

Example:

#include <iostream>
#include <functional>

class BASE
{
  public:
            virtual void test_f(){std::cout<<"BASE::test_f\n";}
};

class DERIVED:public BASE
{
    public:
            virtual void test_f(){std::cout<<"DERIVED::test_f\n";}
};

int main()
{
  // prints Derived
  void (BASE::*p_base)() = &BASE::test_f;
  DERIVED a;
  (a.*p_base)(); 
  auto f = std::mem_fun(&BASE::test_f);
  f(&a);
  
  // prints Base
  a.BASE::test_f();
  auto callLater = [&a]() { a.BASE::test_f();};
  callLater();  
}
Fedor
  • 17,146
  • 13
  • 40
  • 131
Oliver Dain
  • 9,617
  • 3
  • 35
  • 48