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

Program to compare the two dates given by the user, THIS PROGRAM IS TO COM...

THIS PROGRAM IS TO COMPARE THE TWO DATES GIVEN BY THE USER #include #include #include struct date  {   int dd;   int mm;   int yy;  }; date compare(date d1,date d2)  {

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

write a program to find the area under the curve y=f(x) between x=a and x=b integrate y=f(x)   #include float start_point, /* GLOBAL VARIABLES */

Flowcharts, push and pop operation using array draw flowcharts

push and pop operation using array draw flowcharts

String, A string is said to be "Beautiful"€, if it contains only non repet...

A string is said to be "Beautiful"€, if it contains only non repetitive alphabets

ASCII, A string S is said to be "Super ASCII", if it contains the character...

A string S is said to be "Super ASCII", if it contains the character frequency equal to their ascii values. String will contain only lower case alphabets (''''a''''-''''z'''') and

C/c++ program, luminous jewel polishing necklace,c/c++ programming

luminous jewel polishing necklace,c/c++ programming

Define one's complement operator, Define One's Complement Operator? The...

Define One's Complement Operator? The one's complement operator, occasionally called the "bitwise NOT" or "bitwise complement" operator produces the bitwise one's complement of

Explain the break statement, The break statement The break statement, w...

The break statement The break statement, which was already covered in the switch.. case, can also be used in the loops. When a loop statement is encountered in the loops the co

Describe difference between malloc()/free() & new/delete?, for object, mall...

for object, malloc allocates memory in heap however doesn't invoke object's constructor to initialize the object. new allocates memory & also invokes constructor to initialize the

Radix, a popular joke among computer is to say............

a popular joke among computer is to say............

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