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

Define statements to define the constants, the problem description. The ...

the problem description. The order of the C Program should be as follows: Variables and constants Use #define statements to define the constants. Use arr

Area under Curve, #queWrite a program to find the area under the curve y = ...

#queWrite 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 c

Explain register variable, Explain Register Variable Computers have int...

Explain Register Variable Computers have internal registers, which are used to keep data temporarily, before any operation can be performed. Intermediate results of the calcula

Linear iterative process, (a) Write a recursive procedure (digits n) that c...

(a) Write a recursive procedure (digits n) that computes the number of digits in the integer n using a linear recursive process. For example, (digits 42) should return 2 and (digit

Program that will recursively prompt for input from the user, BACKGROUND: F...

BACKGROUND: FILE SYSTEMS  In a computer, a file system (sometimes written filesystem) is the way in which files are named and where they are placed logically for storage and ret

I need decompile library(.dll or .so) into source code, I need Decompile Li...

I need Decompile Library(.DLL or .SO) into source code Project Description: I am seeking an experienced programmer or anyone who knows a thing or two about reverse engineerin

Is it possible to pass an entire structure to functions, Is it possible to ...

Is it possible to pass an entire structure to functions? Yes, it's possible to pass an entire structure to a function in a call by method style. Some programmers prefer to decl

Academic expert for genetic algorithm, Academic expert for Genetic Algorith...

Academic expert for Genetic Algorithm Project Description: Seeking academic expert for Genetic Algorithm. Skills required are Academic Writing, Algorithm, C++ Programming,

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