C ++ collection -- class member pointer

Source: Internet
Author: User

C ++ collection -- class member pointer

C ++ collection -- class member pointer

Preface

The type of a class member is different from that of a general type. The pointer type of a class member is naturally different from that of a general type. We need to discuss the use of class member pointers.

Body

A class member pointer is a pointer to a non-static member of a class.. Its type includesClass typeAndMember type. Generally, pointers point to objects, while class member pointers point to class members rather than class objects.

It should be noted that class member pointers are not callable objects. To call class members through class member pointers, they must be combined with class objects or class pointers. Static type members belong to classes, and their types are similar to common pointers.

Data member pointer

General Declaration Form:

Member type classname: * p;

Its value assignment form:

P = & classname: class data member;

Here the get URL operator & is required.

 

#include 
 
  using namespace std;class Myclass{public:int a;const char c;Myclass(int a, int c) :a(a), c(c){}};int main(void){int Myclass::*pa;pa = &Myclass::a;const char Myclass::*pc;pc = &Myclass::c;Myclass my(520, 'C'), *p = &my;cout << my.*pa << endl;cout << p->*pa << endl;cout << my.*pc << endl;cout << p->*pc << endl;cin.get();return 0;}
 
Run

 


 

In this example, we define two member pointers: pa and pc. Const must be added to the pc statement. Otherwise, Type Mismatch Error will occur when you assign values later. We also define the class objects and pointers. They access the operator. * And-> * Through the member pointers respectively to access the class member pointers.

 

Function member pointer

Similar to the data member pointer, its declaration must also specify the class type and function member type.

Return type (classname: * pfun) (parameter type list );

 

Its value assignment form:

Pfun = & classname: class function member;

 

#include 
 
  using namespace std;class Compute{public:int add(int a, int b){return a + b;}int sub(int a, int b) const{return a - b;}};int main(void){int (Compute::*pfun1)(int, int);pfun1 = &Compute::add;int (Compute::*pfun2)(int, int) const;pfun2 = &Compute::sub;Compute com, *p = &com;cout << (com.*pfun1)(100, 10) << endl;cout << (p->*pfun1)(100, 10) << endl;cout << (com.*pfun2)(100, 10) << endl;cout << (p->*pfun2)(100, 10) << endl;cin.get();return 0;}
 
Run

 


 

This example will not be explained much. The access qualifier is still valid. Only data and functions that can be accessed outside the class can be called using class member pointers.

 

Mem_fn

Wrap the class function member pointer using mem_fn to return a callable object. Include the header file functional.

 

#include 
 
  #include 
  
   using namespace std;class Compute{public:Compute(int a, int b) :a(a), b(b){}int add(){return a + b;}int sub() const{return a - b;}private:int a;int b;};int main(void){Compute com(100, 10), *p = &com;auto fun1 = mem_fn(&Compute::add);cout << fun1(com) << endl;cout << fun1(p) << endl;auto fun2 = mem_fn(&Compute::sub);cout << fun2(com) << endl;cout << fun2(p) << endl;cin.get();return 0;}
  
 
Run

 

Since the add method has the void parameter, only the Class Object fun (com) or pointer fun (p) can be passed during all calls.

If the member method includes parameters, how can this problem be used? See the following code:

 

#include 
 
  #include 
  
   using namespace std;class Compute{public:int add(int a, int b){return a + b;}int sub(int a, int b) const{return a - b;}};int main(void){Compute com, *p = &com;auto fun1 = mem_fn(&Compute::add);cout << fun1(com, 100, 10) << endl;cout << fun1(p, 110, 10) << endl;auto fun2 = mem_fn(&Compute::sub);cout << fun2(com, 120, 10) << endl;cout << fun2(p, 130, 10) << endl;cin.get();return 0;}
  
 
Run

 

This example shows that if a parameter is included, the parameter can be followed by a Class Object or class pointer.

Mem_fn automatically selects the call. * or-> * based on the input parameter type *:

Compute com, * p = & com;

Auto fun = mem_fn (& Compute: add );

Fun (com); // The input object, which is interpreted as auto padd = & Compute: add; (com. * padd )();

Fun (p); // input pointer, which is interpreted as auto padd = & Compute: add; (p-> * padd )();

 

Bind

Bind the function adapter to bind class function members and return callable objects.

The bind function binding has been described in detail.


 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.