Broad Network


Pointer to Member Operators in C++

C++ Operators – Part 7

Forward: In this part of the series, we look at what is known as pointer-to-member operators.

By: Chrysanthus Date Published: 23 Aug 2012

Introduction

This is part 7 of my series, C++ Operators. In this part of the series, we look at what is known as pointer-to-member operators. There are two pointer-to-member operators. I will not explain only the pointer-to-member operators in this article; I will explain other things that go with it that you may not be familiar with.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Prerequisite
You should have read from this blog, the articles, “C++ Pointers”, “C++ Namespaces Basics” and “Pointers to Classes in C++” or similar articles before reading this one. These three articles are among the two series I have mentioned in the previous parts of this series.

Binding
Imagine that you have two classes, A and B, and class A is a property (object) of class B. You instantiate an object from B. Then you modify the property object of B, which is of class A. You can then bind this property of B to the object of B so that the effects will be seen in an instantiated object of A. We shall see examples below.

The binary operator .*
Let us look at an example that uses the .* operator. The operator consists of the dot followed by the asterisk. Consider the following code:

#include <iostream>
using namespace std;

class A
    {
        public:
        int propA;
     };

class B
    {
        public:
        A propB;
    };

A B::*propBPtr = &B::propB;


int main()
    {
        B b;
        b.propB.propA = 9;

        A a = b .* propBPtr;

        cout << a.propA;

        return 0;
    }

Two classes, A and B are described. Class A has a property, propA, which is of type int. Class B has a property, propB, which is of the class A type. propB is an instantiation of class A. After the class B description in the code, you have the statement,

    A B::*propBPtr = &B::propB;

This statement is an initialization statement. The pointer-to-member operator works with a pointer to propB in class B. This is a special pointer, because it is a pointer to the property in the class description and not to an instantiated object from the class. Note that the property in class B we are talking about here is of type A. The class description is similar to a namespace description. So to access a property of a class, you need the scope operator (::) just as you need it to access an identifier in a namespace description.

Let us now look at the initialization statement: On the right of the simple assignment operator you have a reference (&B::propB) to the property in the class B description, which is of type, A. Note the use of the scope operator there. On the left you have the pointer declaration of this property that is in class B description and the pointer is pointing to a property of type A. So the pointer declaration begins with A, then B for class B, the scope operator, then pointer asterisk and the pointer; you can give whatever name you want for the pointer. This statement is a special pointer initialization.

Now let us look at what is in the main function. In main an object of B, called b, is instantiated. The property, propB of the object b is then accessed. This property is of type A. The aim of this access is to give a value to the int property of the property in b. So it is propA of the object, propB of the object b that is actually accessed. So you begin with b, then a dot, then propB, then a dot and then propA. Well, you then have the assignment of an int, which in this case is 9.

The next statement uses the pointer-to-member operator. It binds propB using its pointer to the property in the class B description. The pointer is the second operand of the .* operator and the object b is the first operand of the operator. The result (or return value) of this binding is an object of type A. The result is assigned (copied) to an instantiated object of type A in the code; this object is called, a

Here are quotations from the C++ specification about the binding operator, (.*). “The binary operator .* binds its second operand, which shall be of type “pointer to member of T” (where T is a completely-defined class type) to its first operand, which shall be of class T. The result is an object of the type specified by the second operand.”.

Now the next statement displays the value of the int property of a. This value is 9, which was assigned to the int property of the object of type A that is the property of the class B description. Because of binding, the new instantiated object, a, has the value in its property.

The binary operator ->*
This operator begins with the -> symbol followed by *. The purpose of this pointer-to-member operator is the same as that of the previous operator, but its first operand is a pointer to the b object and not the b object identifier. The following code shows how to use it:

#include <iostream>
using namespace std;

class A
    {
        public:
        int propA;
     };

class B
    {
        public:
        A propB;
    };

A B::*propBPtr = &B::propB;


int main()
    {
        B *bPtr;
        bPtr->propB.propA = 9;

        A a = bPtr ->* propBPtr;

        cout << a.propA;

        return 0;
    }

In the above code, the statement for the pointer-to-member operator is,

        A a = bPtr ->* propBPtr;

The pointer-to-member operators operate from left to right. You have two of them, which are .* and ->* .

That is it for pointer-to-member operators. We take a break here and continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message