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

Using the substitution model illustrate the process, Each of the following ...

Each of the following two procedures defines a method for adding two positive integers in terms of the procedures inc, which increments its argument by 1, and dec, which decrements

How to make a triangle number, i wont to make triangle with number but numb...

i wont to make triangle with number but number from the largest possible number to 01 (reversed).

Visual basic, a program to accept average, if average is 75 to 85 print it...

a program to accept average, if average is 75 to 85 print it out else bypass using counter. enter and process 5 records

How can i present printing for my class fred?, A: Use operator overloading ...

A: Use operator overloading to present a friend left-shift operator, operator #include class Fred { public: friend std::ostream& operator ... private: int i_; // onl

Asset pricing project, In the final project assignment you are asked to dev...

In the final project assignment you are asked to develop an OOP C++ class hierarchy for derivative pricing, using the binomial tree and Black-Scholes option pricing methods. You wi

I need online game dvelopment, Project Description: Online game developm...

Project Description: Online game development Online Live Baccarat Game is needed - client program. - server program(IOCP or ect..). - web program(ASP or PHP or etc..

Make a connection to fifa ultimate team 14 using c++ or c#, I need to creat...

I need to create a connection to FIFA Ultimate Team 14, I want to utilize C++ specifically for the reason that that is the language I have the most experience in. There are a co

Explain binary logical bit-wise operators, Binary logical bit-wise operator...

Binary logical bit-wise operators  There are three logical bit-wise operators :                   &             and                 |              or

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