Member pointer of C ++

Source: Internet
Author: User

If I did not read "C ++ programming ideology", I am afraid that I will not know the member pointer today, which has really opened my eyes. In my understanding, the normal Pointer Points to the address relative to the entire memory space, and the member pointer does not contain the real address, it actually represents the relative position of a member within its class range. A small program can explain the usage of member pointers.

# Include
<Iostream>
Using
Namespace
STD
;

Class
C

{

Public
:

Int
I
,
J
,
K
;
Void
F
()
{

Cout
<
I
<
''
<
J
<
''
<
K
<
'/N'
;
}

};

Int
Main
()
{

C
C
;
C
.
I
=
1
;
C
.
J
=
2
;
C
.
K
=
3
;
C
*
PC
=
&
C
;

Int
C
::*
PM
=
&
C
::
J
;
Cout
<
C
.
*
PM
<
PC
-> *
PM
<
'/N'
;

Void
(C
::*
PMF
) () =
&
C
::
F
;
(C
.
*
PMF
)();
(PC
-> *
PMF
)();
Return
0
;
}

The code output is 22 and 1 2 3. PM is defined as a pointer to Class C member J. When PM is applied to object C, the value of member J of the C object is output. Of course, it cannot be used for private members. PMF points to the member function f of C. When PMF is called on Object C, the member function f is called. The principle is not complex, but it is unclear where such a mechanism is applicable.

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.