C + + supplements-class member pointers

Source: Internet
Author: User

C + + supplements-class member pointers

Preface

The type of a class member differs from the generic type, and the pointer type of the class members is naturally different from the generic type. It is necessary for us to explore the use of the following class member pointers.

Body

A class member pointer is a pointer that can point to a non-static member of a class . Its type includes the class type and the member type to which it is pointing. In general, pointers point to objects, while class-member pointers point to class members rather than class objects.

It should be noted that a class member pointer is not a callable object, and that you want to call a class member through a class member pointer, combining a class object or a class pointer. A static type member belongs to a class, and the type is similar to a normal pointer.

data member Pointers

General form of declaration:

Member type classname::* p;

It is assigned in the form of:

p = &classname:: class data member;

Here the fetch address character &, is necessary.

#include <iostream>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 = &am P 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. The declaration of the PC must be const, otherwise the type mismatch error will occur at a later assignment. We also define the classes ' objects and pointers, which access the operators by member pointers, respectively. * and->*, access to class member pointers.


function member Pointers

Like a data member pointer, its declaration also specifies the class type and the function member type.

return type (classname::* pfun) (List of parameter types);

It is assigned in the form of:

Pfun = &classname:: class function member;

#include <iostream>using namespace Std;class compute{public:int Add (int a, int b) {return a + B;} int sub (int a, int b) Const{return A-A;}}; int main (void) {int (Compute::* pfun1) (int, int);p fun1 = &compute::add;int (Compute::* pfun2) (int, int) const;pfun2 = & Amp Compute::sub; Compute com, *p = &com;cout << (com.*pfun1) (+) << endl;cout << (p->*pfun1) (+) << ; Endl;cout << (COM.*PFUN2) (Ten) << endl;cout << (p->*pfun2) (+) << endl;cin.get (); return 0;}
Run



This example will not explain much. Just one sentence: Access qualifiers are still valid, and data and functions that can be accessed outside of the class can be called using class member pointers.


Mem_fn

The class function member pointer is wrapped by MEM_FN to return a callable object. When used, contains the header file functional.

#include <iostream> #include <functional>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 (+), *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


Because the Add method is the argument is void, all calls are passed only to the class object Fun (COM) or pointer Fun (p).

If the member method is parametric, how do you use it? Look at the following code:

#include <iostream> #include <functional>using namespace Std;class compute{public:int Add (int a, int b) { return a + b;} int sub (int a, int b) Const{return A-A;}}; int main (void) {Compute com, *p = &com;auto fun1 = MEM_FN (&compute::add); cout << fun1 (COM, +) << Endl;cout << fun1 (P, Max, ten) << Endl;auto fun2 = MEM_FN (&compute::sub) cout << fun2 (COM, 120, 10) & lt;< endl;cout << fun2 (p, N, ten) << endl;cin.get (); return 0;}
Run


This example shows that if a parameter is present, the argument follows the class object or the class pointer.

MEM_FN automatically selects calls based on the type of parameter passed in. * or->*:

Compute com, *p = &com;

Auto fun = MEM_FN (&compute::add);

Fun (COM); Incoming object, this sentence will be interpreted as auto Padd = &Compute::add; (Com.*padd) ();

Fun (p); //Incoming pointer, this sentence will be interpreted as auto Padd = &Compute::add; (P->*padd) ();


Bind

Use the function adapter bind, bind the class function member, and return the callable object.

In the BIND function binding, there is already a detailed description.




Directory of this column

    • C + + Supplements directory

Directory of all content

    • CCPP Blog Directory

C + + supplements-class member pointers

Related Article

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.