Details the member access operator and pointer to member operator in C++

  • 2020-05-07 20:12:05
  • OfStack

member access operators:. And - >

grammar

          postfix-expression
          . name
postfix - expression � > name
note
Member access operators. And - > Used to refer to members of structures, unions, and classes. The member access expression has the value and type of the selected member.
There are two forms of member access expressions:
In the first form, postfix-expression represents the value of a structure, class, or union type, and name names the members of the specified structure, union, or class. The value of the operation is the value of name and is lvalue (if postfix-expression is lvalue).
In the second form, postfix-expression represents a pointer to a structure, union, or class, and name names the members of the specified structure, union, or class. This value is the value of name and is an lvalue. � > The operator dereferences the pointer. Therefore, the expression e > member and (*e).member (where e represents a pointer) will produce the same result (the overloading operator, the pool) > Or * except).
The following example demonstrates two forms of a member access operator.


// expre_Selection_Operator.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

struct Date {
  Date(int i, int j, int k) : day(i), month(j), year(k){}
  int month;
  int day;
  int year;
};

int main() {
  Date mydate(1,1,1900);
  mydate.month = 2;  
  cout << mydate.month << "/" << mydate.day
     << "/" << mydate.year << endl;

  Date *mydate2 = new Date(1,1,2000);
  mydate2->month = 2;
  cout << mydate2->month << "/" << mydate2->day
     << "/" << mydate2->year << endl;
  delete mydate2;
}

In this case, the two values are:


2/1/1900
2/1/2000

pointer to the member operators:.* and - > *

 
grammar

          expression .* expression
expression � > * expression
note
Pointer operators to members (.* and pool > *) returns the value of the specific class member of the object specified on the left side of the expression. The right side must specify the members of the class. The following example shows how to use these operators:


// expre_Expressions_with_Pointer_Member_Operators.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;

class Testpm {
public:
  void m_func1() { cout << "m_func1\n"; }
  int m_num;
};

// Define derived types pmfn and pmd.
// These types are pointers to members m_func1() and
// m_num, respectively.
void (Testpm::*pmfn)() = &Testpm::m_func1;
int Testpm::*pmd = &Testpm::m_num;

int main() {
  Testpm ATestpm;
  Testpm *pTestpm = new Testpm;

// Access the member function
  (ATestpm.*pmfn)();
  (pTestpm->*pmfn)();  // Parentheses required since * binds
            // less tightly than the function call.

// Access the member data
  ATestpm.*pmd = 1;
  pTestpm->*pmd = 2;

  cout << ATestpm.*pmd << endl
     << pTestpm->*pmd << endl;
  delete pTestpm;
}
Output
m_func1
m_func1

The results are as follows:


1
2

In the previous example, pmfn, a pointer to a member, was used to call the member function m_func1. Another pmd pointer to a member is used to access the m_num member.
The 2 meta operator.* combines its first operand (which must be of class type) with its second operand (which must be of pointer type to member) at 1.
The 2 - byte operator > * combine its first operand (which must be a pointer to an object of class type) with its second operand (which must be a pointer to a member) at 1.
In an expression containing the.* operator, the first operand must be of class type and accessible, while a pointer to a member specified in the second operand or to a member of an accessible type is explicitly derived from and accessible from that class.
In contains � > In the expression of the * operator, the first operand must be a "pointer to a class type" of the type specified in the second operand, or a type explicitly derived from that class.
Consider the following classes and segments:


// expre_Expressions_with_Pointer_Member_Operators2.cpp
// C2440 expected
class BaseClass {
public:
  BaseClass(); // Base class constructor.
  void Func1();
};

// Declare a pointer to member function Func1.
void (BaseClass::*pmfnFunc1)() = &BaseClass::Func1;

class Derived : public BaseClass {
public:
  Derived(); // Derived class constructor.
  void Func2();
};

// Declare a pointer to member function Func2.
void (Derived::*pmfnFunc2)() = &Derived::Func2;

int main() {
  BaseClass ABase;
  Derived ADerived;

  (ABase.*pmfnFunc1)();  // OK: defined for BaseClass.
  (ABase.*pmfnFunc2)();  // Error: cannot use base class to
              // access pointers to members of
              // derived classes. 

  (ADerived.*pmfnFunc1)();  // OK: Derived is unambiguously
               // derived from BaseClass. 
  (ADerived.*pmfnFunc2)();  // OK: defined for Derived.
}

The pointer operator to a member.* or wor > The result of * is an object or function of the type specified in the declaration of the pointer to the member. So, in the previous example, the result of the expression ADerived.*pmfnFunc1() is a pointer to the function that returns void. If the second operand is left, this result is left.
System_CAPS_note note
If the result of a pointer operator to a member is a function, the result can only be used as the operand of the function call operator.


Related articles: