Kiran's Blog

Ramblings from a dehydrated mind…

  • Archives

    • 14,907
  • Advertisements

Pointers to data members..

Posted by kiraninbng on July 7, 2006

Pointers to data members are pointers which help us to manipulate the data members of an object.The syntax is very similar to that of pointer to function,with the addition of the class name followed by the scope resolution operator(::).

Ex :

class CTest
 int nVal;

// pmCTest is a pointer to an int member of CTest
int CTest::*pmCTest;

You can initialize the pointer to data member by giving the address of member of the Class

Ex:  pmCTest = &CTest::nVal;

The data pointed can be manipulated either by using the object or the object pointer,

Ex :

CTest oCTest;
CTest* pCTest = new CTest;

int nTemp = oCTest.*pmcTest; // Using the object
pcTest->*pmcTest = nTemp; // Using the object pointer

In the next post i will talk about pointers to member functions.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: