37. c ++ primer 4th notes, special tools and techniques, class member pointers

Source: Internet
Author: User

1, Member pointer (Pointer to Member) Contains the class type and the member type. The member pointer is only applicable to non-StaticMember.Static class members are not part of any object, so special syntax is not required to point to static members.The member pointer is a common pointer. By specifying the return type of the function, the parameter table (type and number, whether it isConstAnd class to define the pointer of the member function.

2Use Pointer of Class Members

Similar to member access Operators.And->,.*And->Are two new operators that enable us to bind member pointers to actual objects. The left operands of these two operators must be class objects or class pointers, and the right operands are member pointers of this type.

• Member pointer unreferencing Operator(.*)Obtains a member from an object or reference.

• Member pointer arrow Operators(-> *)Get members through object pointers.

ExampleCode

# Include "iostream" # include "string" # include "vector" using namespace STD; Class Screen {public: screen (STD: String strcon = "here", STD :: string: size_type mycursor = 12): contents (strcon), cursor (mycursor) {} typedef STD: String: size_type index; char get () const {return '1';}; char get (index HT, index WD) const {return '2';}; public: STD: String contents; index cursor; index height, width ;}; int main () {// The defined member pointer reads string screen: * ps_screen = & screen: contents; char (screen:: * PMF) () const = & screen: Get; char (screen: * pmf1) (screen: Index, screen: Index) const = & screen :: get; screen myscreen; // use the member function pointer char C1 = myscreen. get (); char C2 = (myscreen. * PMF) (); cout <C1 <"" <C2 <Endl; // 1 screen * pscreen = & myscreen; c1 = pscreen-> get (); C2 = (pscreen-> * PMF) (); cout <C1 <"" <C2 <Endl; // 1 1C1 = pscreen-> get (0, 0); C2 = (pscreen-> * pmf1) (0, 0 ); cout <C1 <"" <C2 <Endl; // 2 2 // use the data member pointer screen: Index screen: * pindex = & screen :: cursor; screen: Index ind1 = myscreen. cursor; screen: Index ind2 = myscreen. * pindex; cout <ind1 <"" <ind2 <Endl; return 1 ;}

Note: ( Myscreen. * PMF )(); Brackets cannot be omitted. Because ()Priority Ratio*It is high, so if parentheses are omitted, It is parsed: Myscreen .* ( PMF () ) ; This Code indicates that the call name is PMFTo bind the return value of the function to the member object operator (.*.

2) Member pointer function table

A common purpose of function pointers and member function pointers is to store them in function tables. A function table is a collection of function pointers, from which you can select a given call at runtime.

Sample Code

# Include "iostream" # include "string" # include "vector" using namespace STD; Class Screen {public: // other interface and implementation members as before // screen & Home () {}; // cursor movement functions // screen & forward () {}; // screen & back () {}; // screen & up (){}; // screen & down () {}; int home () {return 1 ;}int forward () {return 1 ;}int back () {return 1 ;} int up () {return 1 ;}int down () {return 1 ;}public: // other interface and implementation members as before // action is pointer that can be assigned any of the cursor Movement members // typedef screen & (screen: * Action )(); typedef int (screen: * Action) (); static Action menu []; // function tablepublic: // specify which direction to moveenum directions ctions {home, forward, back, up, down}; screen & move (directions);}; screen & screen: Move (direcm cm) {// fetch the element in menu indexed by CM // run that member on behalf of this object (this-> * menu [cm]) (); return * This ;} screen: Action screen: menu [] ={ & screen: Home, & screen: Forward, & screen: Back, & screen: Up, & screen :: down,}; int main () {screen myscreen; myscreen. move (screen: Home); // invokes myscreen. homemyscreen. move (screen: Down); // invokes myscreen. downreturn 1 ;}

3, Enumeration size

Sample Code

# Include "iostream" # include "string" # include "vector" using namespace STD; Class A {public: Enum mydata {M1, M2, M3, M4 }; static int IMy ;}; int A: IMy = 1; Enum mydata {M1, M2, M3, M4}; int main () {A data1; cout <:: m1 <"<A: M2 <" "<A: M3 <" <Endl; // 0 1 2 cout <data1.m1 <"" <data1.m2 <"" <data1.m3 <Endl; // 0 1 2 // cout <:: mydata <Endl; // "A: mydata": Use this type as an invalid cout expression <sizeof (a) <Endl; // 1 cout <sizeof (data1) <Endl; // 1 cout <sizeof (mydata) <Endl; // 4 mydata data2; cout <sizeof (data2) <Endl; // 4 return 1 ;}

The enumerated size is the size of an integer. However, in a class, when the size of the class is calculated, the size of the data member is not calculated, and the size of the enumeration member is also not calculated. Enumeration data members defined in the class can be directly referenced through the class name and scope.

Http://www.cnblogs.com/mydomain/archive/2011/04/30/2033483.html

4, We need to note that the static function does notThisPointer. However, classes and class objects share the static data members defined in the class. In non-static functionsYou can useThisPointer to reference these static data members.

Sample Code

# Include "iostream" # include "string" # include "vector" using namespace STD; Class A {public: void print () {cout <this-> IMy <Endl;} public: static int IMy ;}; int A: IMy = 1; int main () {A data1; data1.print (); // 1 cout <A: IMy <Endl; // 1data1. IMy = 2; A data2; cout <data2.imy <Endl; // 2 return 1 ;}

Http://www.cnblogs.com/mydomain/archive/2011/03/22/1991449.html

5, Function pointer assignment

Sample Code

 
# Include <iostream> using namespace STD; char myfun () {return '1';} Char * myfun2 () {char * P = (char *) malloc (3 ); return P;} int main () {char (* p) () = myfun; // char * (p1 () = & myfun2; // error, cannot be converted from "char * (_ cdecl *) (void)" to "char * (void) char * (* P1) () = & myfun2; // char * P () = myfun (); // note that this is incorrect. P is defined as a function and returns char * instead of a function pointer. Return 1 ;}

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.