1.5. Pointers to components of class 1.5.1. Pointers to components - data

Lecture



You can define a pointer to data components.

datatype (class_name:: * pointer_name)

You can include its initializer in the definition of a pointer.

& class_name:: component_name

Example:

double (complex:: * pdat) = & complex:: re;

Naturally, in this case, the data members must have the status of open (pubic). After the pointer is initialized, it can be used to access the data of the object.

complexc (10.2,3.6); c / * pdat = 22.2; // the value of the re field of the object c has changed.

A pointer to a class component can be used as an actual parameter when calling a function. If pointers to the object and to the component are defined, then access to the component using the operation

'-> *'. pointer_to_object -> * pointer_to_component ''

Example 1.5.1

double (complex:: * pdat) = & complex:: re;

complex C (10.2,3.6);

complex * pcom = & C;

pcom -> * pdat = 22.2;

// You can determine the type of the pointer to the class data-components:

typedef double (complex :: * PDAT);

void f (complex c, PDAT pdat) {c. * pdat = 0;}

complex c;

PDAT pdat = & complex :: re; f (c, pdat);

pdat = & complex :: im; f (c, pdat);

created: 2015-12-20
updated: 2026-03-10
263



Was this answer useful?
Choose a quick rating so we can improve the next answer for you.
How satisfied are you?


Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "C ++ (C plus plus)"

Terms: C ++ (C plus plus)