Class member pointer classname: * Member, (classname: * PF )()

Source: Internet
Author: User
Review:

Definition of common function pointers1 Int(*PFI )()

Question:
Suppose there are the following classes: 1 Class Screen
2 {
3 Public :
4 Int Height (){ Return _ Height ;}
5 Int Width (){ Return _ Width ;}
6 // .
7 }

Now assign a value1 PFI= &Screen: height;
2 //Illegal value assignment, type violation

Because the pointer to a member function contains three aspects: 1) parameter table (number, type) 2) return type 3) class type
The normal pointer to a function contains only two aspects.

correct pointer definition to a member function: 1 int (screen :: * PMF) ();
2 PMF = & screen: height;
3
4

To avoid complex pointer types, use typedef1 TypedefInt(Screen*Ptr_type )();
2 Ptr_type PMF= &Screen: height;

Note:Classname ::*Syntax

Usage of pointers to class members: 1 Int (Screen :: * PMF )() =   & Screen: height;
2 Screen myscreen, * Bufscreen;
3
4 // Directly call member functions
5 If (Myscreen. Height () = Bufscreen -> Height ())
6 //
7
8 // Equivalent call through the member function pointer
9 If (Myscreen. * PMF )() = (Bufscreen -> * PMF )())
10 //

In the same way, point to the data member pointer:1 IntScreen ::*Ps_screen= &_ Height

Conclusion: Compared with common pointersClassname ::*

Static member pointer:
We know that static class members belong to global objects and functions of the class, and their pointers are normal pointers (remember that static member functions do not use this pointer)

1 Class Classname
2 {
3 Public :
4 Static   Void Rai ( Double Incr );
5 Static   Double Interest (){};
6 //
7 Private :
8 Static   Double _ Interestrate;
9 String _ Owner;
10 }
11
12 // OK
13 Double   * PD =   & Classname: _ interestrate;
14 // Error
15 Double Classname :: * PD =   & Classname: _ interestrate;
16
17 // OK
18 Double ( * Pfunc )() =   & Classname: interest;

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.