Friday 17 April 2015

Accessing Data Members Through Objects


C++ provides .* exclusively for use with pointers to members called member deference operator. This operator is used to access class members using a pointer to members and it must be used with the objects of the class. For example

Class X
{
int y;
public:
int a;
int b;
}
void main()
{
X Obj;
int X::*ptr; //
ptr = &X::a; // int X::*ptr = &X::a;
Obj.*ptr = 20;
int k = Obj.*ptr;
}

No comments:

Post a Comment