Pointers to class members

Source: Internet
Author: User

C++language, you can describe a pointer to a data member of a class and a pointer to a member function of a class. Both of these pointers must be used in conjunction with the object or pointer to the object. 1A pointer to a data member of a class is defined in the format: type name Class Name::*pointer; This description does not say that the pointer belongs to a class, but rather that the pointer can only point to a member of the specified type of the specified class. 2pointer to a class's member function type name (class name::*pointer) (parameter table); Similarly, a function pointer does not belong to a class, but to a function that can only point to a specified prototype of a class. #include<iostream.h>classa{ Public:    intA; intb; A () {}intf () {intA::* p=&a::a;  This->*p= -;//You must use the this pointer        return 0; }    };voidMain () {int(A:: *q)    ();    A Obja; intA::* p=&a::b; Obja.*p=Ten; cout<<obja.b<<Endl; Q=A::f; (Obja.*Q) ();//note the preceding parenthesescout<<obja.a<<Endl;}

Pointers to class members

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.