Pointer to a class member

Source: Internet
Author: User

PS : Const object can only be called Const Function!! Non- const objects casually!!

Member pointers are applied only to non- static members of the class. The static class member is not part of any object, so no special syntax is required to point to the static member, and thestatic member pointer is a normal pointer .

int *p = &Screen::total; (Total is the static int total;)

Example:

#include <iostream>

using namespace Std;

Class screen{

Public

static int total;

int sum;

Screen (): Sum (2) {}

int a (int i, int j) {

cout<<244<<endl;

return 8;

}

};

int screen::total = 6;

int main () {

Screen SC;

int *ptr = &Screen::total;

int screen::* P1 = &Screen::sum;

Int (screen::* p2) (int, int) = &Screen::a;

cout<<sc.*p1<< "* *" <<sc.total<< "* *" << (SC.*P2) <<endl;

cout<<*ptr<<endl;

return 0;

}

Output:

244 ---------- come out first.

2**6**8

L Declare member pointers

Class Screen {

Public

typedef STD::STRING::SIZE_TYPE Index;

Char get () const;

Char get (Index HT, index WD) const;

Private

std::string contents;

index cursor;

index height, width;

};

ü Define a pointer to a data member

such as contents

String screen ::*ptr = & Screen::contents;

Screen::index Screen :: *p = & Screen::cursor;

ü Defines a pointer to a member function

A pointer to a member function must match the type of the function it refers to in three ways:

1. the type and number of function parameters, including whether the member is Const.

2. return type.

3. the type of the owning class.

such as get ()

char (screen::*p) () const = & Screen::get;

char (screen::*p) (screen::Index, Screens::index) const = & Screen::get;

ü Use type aliases for member pointers

typedef char (screen::* Action) (Screen::index, screen::index) const;

Action p = &Screen::get;

L Use class member pointers

? the Member pointer dereference operator (. *) gets a member from an object or reference.   

PS: cannot be overloaded with operators (:: . . *?:)

Mandatory member function (= [] () )

must friend (<< >> )

? the Member Pointer Arrow operator (->*) obtains a member through the object's pointer.

ü Using member function pointers

char (screen::* PMF) () const = &Screen::get;

Screen Myscreen;

Char C1 = Myscreen.get ();

char C2 =(MYSCREEN.*PMF)(); //equivalent call to get

Screen *pscreen = &myScreen;

C1 = Pscreen->get ();

C2 = (PSCREEN->*PMF)(); //equivalent call to get

Calls (MYSCREEN.*PMF) () and ( PSCREEN->*PMF) () require parentheses because the calling operator (()) has a higher precedence than the member pointer operator.

ü Using data member pointers

Screen::index screen::* Pindex = &Screen::width;

Screen Myscreen;

Screen::index ind1 = myscreen.width;

Screen::index ind2 = myscreen.*pindex; //dereference to get width

Screen *pscreen;

Ind1 = pscreen->width;

Ind2 = pscreen->*pindex; Dereference pindex to get width

PS: do not add parentheses!!!

The pointer to the **************** member function differs from the pointer to the normal function ************

Pointers to member functions:

char (screen::* PMF) () const = &Screen::get; //Must have &

use is (MYSCREEN.*PMF) () or (PSCREEN->*PMF) ()

Pointers to normal functions:

char (*PMF) () const = &get; or char (*PMF) () const =get;

use is PMF () or (*PMF) ()

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Pointer to a class member

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.