Pointer to class members and member functions

Source: Internet
Author: User

1Pointer to a class member

1.1Concept

Unlike a regular pointer, a pointer to a class member does not point to a specific position. It points to a specific member of a class rather than a specific member of a specific object. Usually, the pointer pointing to the data member is viewed as an offset. Most compilers implement a pointer to a data member into an integer that contains the offset of the member to which the data member is directed, add 1 (add 1 to allow 0 to represent an empty data member pointer ). This offset tells you how many bytes the position of a specific member is from the start point of the object. The syntax for declaring a class member pointer is as follows:

Type classname: * var;

Eg:

Class C

{

Public:

Int _;

};

Int C: * pimc; // a pointer pointing to an int member of C

C AC;

C * Pc = & AC;

Picmc = & C: _;

AC. * pimc = 0;

Int B = pc-> * pimc;

When the value of pimc is set to & C: A _, pimc is actually set to the offset of A _ in C. Unless a _ is a static member, the expression & C: A _ 'does not bring an actual address, but an offset. This offset applies to any object of type C, that is, any object generated by type C can use pimc. When you write down PC-> * pimc, you actually add the address in the PC to the offset in pimc. When AC. * pimc is written, the address in a is added with the offset in pimc.

1.2Features

The pointer to a class member has an implicit conversion from a pointer to a base class member to a pointer to a common class member, however, there is no conversion from a pointer to a derived class member to a pointer to any of its base class members.

In C ++, there is a predefined conversion from a pointer to a derived class to any of its common base classes, and the pointer to a class member is the opposite. For example, the circle class inherits the shape class. A circle * type pointer can be converted to a shape * type pointer.

Class shape

{

Point center;

};

Class circle: Public shape

{

Double Radius;

}

A pointer to a Circle member cannot be converted to a pointer to a shape member because the pointer to a class member is an offset, the parent class, such as shape, may not contain this member in the subclass, such as the radius in the circle. On the contrary, a pointer to a parent class member can be converted to a pointer to a Child class member. Because the Child class inherits the members of the parent class, any offset of the parent class is equally valid in the Child class.

2Pointer to a member function

When obtaining the address of a non-static member function, the obtained address is not an address, but a pointer to the member function.

Class shape

{

Public:

Void moveTo (point location );

Bool validate () const;

};

Class circle: Public shape

{

Bool draw () const;

};

Void (shape: * mf1) (point) = & shape: moveTo;

Bool (shape: * mf2) () = & shape: Validate;

Unlike pointers to common functions, classname is added before the variable name ::. Like a pointer to a data member, an object or a pointer to an object is required to dereference a pointer to a member function.

Circle CIRC;

Shape * pshape = & CIRC;

(Pshape-> * mf2) (); // call shape: Validate

(CIR. * mf2) (); // call shape: Validate

-> * And. * must be added with () because they have lower priority than () operators.

Pointers to member functions are the same as pointers to data members. For example:

Class B

{

Public:

Void bset (INT Val) {bval = val ;}

PRIVATE:

Int bval;

};

Class D: Public B

{

Public:

Void dset (INT Val) {dval = val ;}

PRIVATE:

Int dval;

};

B;

D;

Void (B: * F1) (INT) = & D: dset; // Error

(B. * F1) (12); // error. dval does not exist in B.

Void (D: * F2) (INT) = & B: bset; // OK

(D. * F2) (11); // OK

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.