home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / tctnt / f.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  1.2 KB  |  42 lines

  1. /* F.CPP: Pointers and Members Functions
  2.  
  3.     The following code demonstrates defining a pointer to a member
  4.     function, and defining a member of a class to be a pointer to a
  5.     member function of another class.
  6. */
  7.  
  8. #include <iostream.h>
  9.  
  10. class A {
  11.     public:
  12.         void foo(void) {
  13.             cout << "\nin A::foo";
  14.     }
  15. };
  16.  
  17. class B {
  18.   public:
  19.     void(A::*ptr)(void); //pointer to member function of class A
  20. };                       //that takes void parameters and returns
  21.                          //void, defined in class B
  22.  
  23. //*******************************************************************
  24. int main()
  25. {
  26.   B xx;
  27.   A yy;
  28.  
  29.   void(A::*ptr2)(void); // ptr to member function of class A
  30.                         // defined locally
  31.   ptr2=&A::foo;         // assign addr of member function to variable
  32.  
  33.   (yy.*ptr2)();         // call member function using local definition
  34.  
  35.   xx.ptr=&A::foo;       // assign address of member function to member
  36.                         // of class B, which is a ptr to a member
  37.                         // function of class A.
  38.  
  39.     (yy.*(xx.ptr))();     // call member function using variable in class B
  40.     return 0;
  41. } // end of main()
  42.