Pointers to members, C/C++ Programming

Pointers to Members:

In C a pointer is created  to locate  the address of another variable  for faster access.

Consider the following example. int x, *p;

p = &x;  Pointer p is pointing to the address of x, remember a pointer is also a variable and it has its own address.  The above technique is fine as long as the variable is of primary data type not in the class.  If the primary data type is inside the member of a class which is known as data member then the indirection operator * is not valid, instead operator ::* must be used.

Rules for declared pointer to the class and its member:

1.  Only Class and its member can be declared as pointer (M *pm).

2.   Members are declared as pointer through its class (int M::* px).

3.   When class is declared as pointer members must also be declared as pointer to access the member.

4.   When only members are declared as pointer the member can be accessed using given synatax  (object_name.*pointer-to-member)

5.   When class is declared as pointer remember member must also be a pointer such member can be accessed with given syntax (object_name->*pointer-to-member)

6.   Only Member function are declared as pointer void(M::*pf)(int,int), for member function the pointer must be within( ) and it must specify type of argument just like function.

7.   Using only member function as pointer  (op.*pf)(30,40);

8.   Class and member function declared as pointer accessing is (op->*pf)(30,40);

Posted Date: 9/20/2012 8:01:41 AM | Location : United States

Related Discussions:- Pointers to members, Assignment Help, Ask Question on Pointers to members, Get Answer, Expert's Help, Pointers to members Discussions

Write discussion on Pointers to members
Your posts are moderated
Related Questions
Question : (a) Define a Pointer. Provide an example of an integer pointer variable. (b) Give advantages of using pointers over arrays. (c) Declare an integer pointer

Write a recursive function recursiveMin that takes an integer array, a starting subscript and an ending subscript as arguments, and returns the smallest element in the array. The

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

decode smugglers are very smart in day by day

Project Description: I want a simple program that will spoof my hardware id. I should be able to prepare the id to spoof to in the program and press "random" button to spoof to

Want an EA project for Jack Bsher Project Description: New EA game Skills required   Android, C++ Programming, PHP, Metatrader, SQL

Need help finishing my homework. It''s been 5 years since I''ve use C++. I am a VN.Net programmer. -Todd

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Inline functions, C++ provides inline functions to help reduce function_call overhead especially for small functions. The qualifier inline before function's return type in the f