f



Pointer To Member Functions, Is This Kind of Use SAFE?

Hi All,

I am implementing a framework. In my framework, I want to define a set of 
handler classes,
deriving from a common base class. Then in the handler classes, I would like 
to define a
set of handler functions, all have the same signature, to say, all have the 
signature like:
void f(void *)

Then in another loop, I would instantiates those handlers and dynamically 
choose a handler
function in a handler class to fulfill a task. Since the concrete handler 
and the name of the
handler functions cannot be pre-determinated, I would like to pass in a 
pointer to the object
of a handler class and a pointer to member function to the caller, thus in 
the caller, I could
compose those two pointers up to call the desired handler function.

The problem, is then, how could I achieve this under a unified interface? I 
defined a pointer
to member function type in the Base class scope and try to assign pointer to 
derived class's
member functions to variable of this type. Although I have tried this in 
both G++ 3.2 and VC7,
I still doubt if such trick is safe, anybody could give me some suggestions 
and hints? And
I would appreciate all of your helps.

Following is a sample code I have used for test under G++ and VC7. Thanks in 
advance.

#include <iostream>
using namespace std;

class Base{
public:
  Base(){}
  virtual ~Base(){}

  virtual void v() = 0;
};

typedef void (Base::*MemberFunc)(void);

class Derived1 : public Base{
public:
  void f(){
    cout << "This is in f." << endl;
  }

  void g(){
    cout << "This is in g." << endl;
  }

  void v(){
    cout << "This is in Derived1: v." << endl;
  }
};

class Derived2 : public Base{
public:
  void h(){
    cout << "This is in h." << endl;
  }

  void m(){
    cout << "This is in m." << endl;
  }

  void v(){
    cout << "This is in Derived2: v." << endl;
  }
};

class Leaf : public Derived2{
public:
  void n(){
    cout << "Hello, world, in n." << endl;
  }

  using Derived2::v;
};

int main(){
  MemberFunc fp;
  Base *pBase;

  Derived1 d1;
  Derived2 d2;
  Leaf l;

  cout << "Test Derived1 ..." << endl;
  pBase = &d1;
  fp = static_cast<MemberFunc>(&Derived1::f);
  cout << "fp to Derived1::f is: " << fp << endl;
  (pBase->*fp)();

  fp = static_cast<MemberFunc>(&Derived1::g);
  cout << "fp to Derived1::g is: " << fp << endl;
  (pBase->*fp)();

  fp = static_cast<MemberFunc>(&Derived1::v);
  cout << "fp to Derived1::v is: " << fp << endl;
  (pBase->*fp)();


  cout << "Test Derived2 ..." << endl;
  pBase = &d2;
  fp = static_cast<MemberFunc>(&Derived2::h);
  cout << "fp to Derived2::h is: " << fp << endl;
  (pBase->*fp)();

  fp = static_cast<MemberFunc>(&Derived2::m);
  cout << "fp to Derived2::m is: " << fp << endl;
  (pBase->*fp)();

  fp = static_cast<MemberFunc>(&Derived2::v);
  cout << "fp to Derived2::v is: " << fp << endl;
  (pBase->*fp)();

  cout <<"Test Leaf ..." << endl;
  pBase = &l;
  fp = static_cast<MemberFunc>(&Leaf::n);
  cout << "fp to Leaf::m is: " << fp << endl;
  (pBase->*fp)();

  fp = static_cast<MemberFunc>(&Leaf::v);
  cout << "fp to Leaf::v is: " << fp << endl;
  (pBase->*fp)();

  return 0;
}


=============================
output:
=============================
Test Derived1 ...
fp to Derived1::f is: 1
This is in f.
fp to Derived1::g is: 1
This is in g.
fp to Derived1::v is: 1
This is in Derived1: v.
Test Derived2 ...
fp to Derived2::h is: 1
This is in h.
fp to Derived2::m is: 1
This is in m.
fp to Derived2::v is: 1
This is in Derived2: v.
Test Leaf ...
fp to Leaf::m is: 1
Hello, world, in n.
fp to Leaf::v is: 1
This is in Derived2: v.

========================================
BTW: it's queer that the output of "fp to XXXX is:" lines are always 1,
both in output of G++ and VC. Anybody could explain this to me? 



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Neal
11/18/2004 12:08:29 AM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8506) is leader. Post Follow

2 Replies
294 Views

Similar Articles

[PageSpeed] 24

Neal Chen wrote:

 > Hi All,
 >
 > ...
 >
 > ========================================
 > BTW: it's queer that the output of "fp to XXXX is:" lines are always 1,
 > both in output of G++ and VC. Anybody could explain this to me?
 >

  cout << "fp to Leaf::v is: " << fp << endl;

should be

  cout << "fp to Leaf::v is: " << (void *)fp << endl;

printing function pointer using ostream will always give output '1'

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
syro555
11/19/2004 4:54:44 PM
"Neal Chen" <zeusnchen@msn.com> wrote in message news:<cnfipp$vs4$1@news.yaako.com>...
 > Hi All,
..............
 > The problem, is then, how could I achieve this under a unified interface? I
 > defined a pointer
 > to member function type in the Base class scope and try to assign pointer to
 > derived class's
 > member functions to variable of this type. Although I have tried this in
 > both G++ 3.2 and VC7,
 > I still doubt if such trick is safe, anybody could give me some suggestions
 > and hints? And
 > I would appreciate all of your helps.

Well, static_cast works in this case?
if you are sure that object and function
pointers matches right type, then it is safe.

Greetings, Bane.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
bmaxa
11/19/2004 4:55:52 PM
Reply: