Pointer declaration for member function, C/C++ Programming

Assignment Help:

Pointer declaration for member function:

Method 1: When Class is not declared as pointer

M n;

void(M::*pf)(int,int)=&M::setxy; (n.*pf)(10,20);

n.setxy(10,20); //is same as prior statement.

Remember in C the syntax is (*function_name)(arguments); in C++ the only difference is the function should be member function of a class therefore the function should be identified by the class with           help      of              scope    operator         ::,             thus             the     syntax   for       C++          is (class_name::*function_name)(argument);

 

 

Method 2: When Class is declared as pointer

M *op;

op=&n;

(op->*pf)(30,40);

 

The difference from the first method is the class is also declared as pointer class, in such case the dereferencing operator -> is used (pointer_class_name:_*pointer_function_name)(argument);

 

The following example will give clear understanding of pointer to members both function members and data member.

 

class M

{int x, y;

public:

void setxy(int a, int b)

{x=a;

y=b;

}

friend int sum(M m);

};

int sum(M m)

{//Declaring pointer to data member int M::*px;

px=&M::x; int M::*py; py=&M::y;

//Declaring class as pointer

M *pm;

pm=&m;

int S,S1,S2;

//Two ways of reading through pointer

S1=m.*px; //object_name.*pointer-to-member function

S2=pm->*py;//pointer-to-object->*pointer-to-member function

S=S1+S2;

//S=m.x+m.y; //is same as prior statement without pointer.

return S;

}

int main()

{clrscr();

M n; void(M::*pf)(int,int)=&M::setxy; (n.*pf)(10,20);

n.setxy(10,20); //is same as prior statement. cout<<"SUM is "<<sum(n)<<"\n";

 

M *op;

 

 

op=&n;

(op->*pf)(30,40);

//n.setxy(30,40); //is same as prior statement. cout<<"Sum is "<<sum(n);

return 0;

}


Related Discussions:- Pointer declaration for member function

Explain the standard input/output iostream library, Question 1 Implemen...

Question 1 Implement a class stack which simulates the operations of the stack allowing LIFO operations. Also implement push and pop operations for the stack 2 Explain the c

Write a program to compute the equation, Write a program to compute the fol...

Write a program to compute the following equation for values of time.           y = 2x+3   The values of time are stored in the file program.dat and the solution should be display

Menus, create a shopping cart in c++

create a shopping cart in c++

Minimization and karnaugh maps, There are formal ways of reducing Boolean e...

There are formal ways of reducing Boolean expressions in order to minimize the logic circuit. The two elementary ways of minimization are using Boolean expressions/De Morgan Theore

Program to calculate tax - c++, Program to calculate tax: float tax (f...

Program to calculate tax: float tax (float) ; int main() {                 float purchase, tax_amt, total;                 cout                 cin >> purchase

Fundamental input - output routines getc and putc, Access to the channel/de...

Access to the channel/devices is achieved by means of general purpose I/O routines Theses are standard functions described in stdio.h header file namely getc and putc. Getc and put

Calculate the average assignment grade, 1. The main program must be in a fi...

1. The main program must be in a file called A4.cpp 2. The data must be read in from a data file.  The user must enter the filename.  A sample data file will be provided on Mood

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

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.   #include float start_poin

What if one can''t wrap the local in an artificial block?, What if one can'...

What if one can''t wrap the local in an artificial block?

Define how passing arrays to a function, Define How Passing Arrays to a Fun...

Define How Passing Arrays to a Function? A complete array can be passed to a function as an argument. The manner in which the array is passed be different from that of an ordin

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