Pointers to members, C/C++ Programming

Assignment Help:

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);


Related Discussions:- Pointers to members

Command line program to find name matches, What's a six-letter word that ha...

What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than a

What is the maximum number of constructors, Question: (a) (i) Explain...

Question: (a) (i) Explain how class members can be accessed when using objects of the class. Use a suitable example to illustrate your answer. (ii) Explain how the privat

Described access privileges in c++? what is the default , A: In C++ the acc...

A: In C++ the access privileges are public, private and protected. The default access level assigned to members of a class is private. Private members of any class are accessible o

Integration, Write a program to find the area under the curve y = f(x) betw...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Wap to print sum=1!+2!+3!+4!+....n!, WAP TO PRINT SUM=1!+2!+3!+4!+....N! ...

WAP TO PRINT SUM=1!+2!+3!+4!+....N! #include stdio.h> #include conio.h>   void main() {                    int n,i;                    float sum=0,fac

What does it mean to declare a member function as a virtual, What does it m...

What does it mean to declare a 1.      member function as a virtual A: (a) C++ virtual function is member function of any class, whose functionality may be over- ridden in

Program to track the hours an employee worked , Description: Create a...

Description: Create a program that allows the user to track the hours an employee worked in a week. How much the employee was paid and any extra hours worked (overtime pay).

Working of ordered linked list, Working Ordered linked list: • Eachint...

Working Ordered linked list: • Eachinteger in the queue is stored inside of a QueueItem. The QueueItem contains the integer, and a pointer to the next item in the queue. Fo

Define bitwise-shift operators, Define Bitwise-Shift Operators? The shi...

Define Bitwise-Shift Operators? The shift operators perform suitable shift by operator on the right to the operator on the left. The right operator should be positive. The va

Windows object code copy minder defeat, Project Description: We own prop...

Project Description: We own proprietary software which long ago had Copyminder protection added. We no longer have the source code or a relationship with the original coder and

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd