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

Program that computes square matrix multiplication, Goal: Design a progr...

Goal: Design a program that computes square matrix multiplication on GPU using CUDA. Write the code in C. In particular, your implementation should obey the following requiremen

Luminous Jewels - The Polishing Game, Byteland county is very famous for lu...

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

Write a program that calculates circumference and area, Write a program cal...

Write a program called A1Q3, that reads it the radius of a circle as an integer and prints the circle's diameter, circumference and area.  Use a constant value for pi.  Do all calc

Write a constructor for a movie, Suppose we use Lists to represent a movie ...

Suppose we use Lists to represent a movie which has a title, cast and year. For example, the following list represents the movie, "Die Hardly", with the cast "Bruce Willing" and "T

Explain the returning references from functions, Returning References from ...

Returning References from Functions Just as in passing the parameters by reference, returning a reference also doesn't return back a copy of the variable , instead an alias is

Write a program in c++ to read n numbers in an array, Problem Write a p...

Problem Write a program in C++ to read N numbers in an array, the user should be able to search a particular number in the array using sequential search algorithm. Writing a

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