C++ pointer to members

C++ Pointer to members

This is possible to take the address of a member of a class and assign it to a pointer. The address of a member can be obtained by applying the ampersand operator & to a “fully qualified” class member name. A class member pointer can be declared using the operator ::* with the class name

for example given the class

class X
{
private:
int m;
public:
void show();
};

we can define a pointer to member m as follows:
int X::* ip=&X::m;

The ip pointer create thus acts like a class member in that it must be invoked with a class object. in the statement above the phrase X::* means “pointer to pointer of X class “The phase &X::m means the “address of the m member of X class”

Note that the following statement is not valid

int *ip=&m; //won’t work

It is because m is not simply an int data type. it has meaning only whenever it is associated with the class to which it belongs. The scope resolution operator must be applied to both the pointer and the member.

The pointer ip can now be used to access the member m inside member functions(or the friend functions) let us assume that x is an object of X declared in a member function.

We can access m using the pointer ip as follows

cout << x.*ip; // display
cout << x.m; // same as above
now look at the following code
ap=&&x; // ap is pointer to object X
cout << ap->*ip; // display m
cout << ap->m; // same as above

The dereferencing operator ->* is used to access a member when we use pointers to both the object and the member. The dereferencing operator .* is used when the object itself is used with the member pointer.
Note that *ip is used like a member name

Let us take example the use of dereferencing operators to access the class members

DEREFERENCING OPERATORS

#include<iostream>
using namespace std;
class M
{
int x;
int y;
public:
void set_xy(int a,int b)
{
x=a;
y=b;
}
friend int sum(M m);
};
int sum(M m)
{
int M::*px=&M::x;
int M::*py=&M::y;
M *pm=&m;
int s=m.*px+pm->*py;
return s;
}
int main()
{
M n;
void (M::*pf)(int,int)=&M::set_xy;
(n.*pf)(10,20);
std::cout<<"sum=" <<sum(n)<<"\n";
M *op=&n;
(op->*pf)(30,40);
std::cout<<"sum="<<sum(n) <<"\n";
}

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise