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
Explain the Function Prototype? The Functions must be declared before they are used, ANSI C provides for the new function declaration syntax called as the function prototype,

program that counts the number of occurrences of the string in the n-th padovan string p(n)

C program to demonstrate Pointer to variable: void p2a(int *); void main() {                 int x=10, *a,**b;                 int arr[5];                 //poin

I am doing project on home stay which includes features like add homestay,search,book,delete homestay,

Program is to define a class as student: Program is to define a class as student and display the records specific depending upon the number class student  {  private:

algorithm to prepare mark sheet of a student by inputing name,branchcode,semester,register no,5 marks of students and total mark of student

Within programs we very often want to execute a different section of code, depending upon various conditions within C the If statement is used to achieve this. The If statement con

WHAT IS A C LANGUAGE? C IS A PROGRAMMING?

A: It let you to provide an intuitive interface to users of your class, as well as makes it possible for templates to equally work well with classes and built-in/intrinsic types.

obtain two numbers from thekey board,and determain and display(if either)is the larger of two numbers.