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

Write a c program to calculate the equation, Write a C program to calculate...

Write a C program to calculate the output Y for a given value of X for the following formula   Y=X 2 + 2X +3   #include stdio.h   void main()   {   char promt;   float y,x

Memory management system, 1. Basic Heap: Each memory location in our model ...

1. Basic Heap: Each memory location in our model of the RAM will be an instance of type Memory: 2. typedef union Memory_u Memory; 3. union Memory_u{ 4. char character;

I want packet capture analysis, I want Packet capture analysis Project D...

I want Packet capture analysis Project Description: Need assistance in debugging some packet capture Skills required is C Programming

Common c control flow instructions, In this Lab you will code 8086 assemble...

In this Lab you will code 8086 assembler equivalents (Virgo) for common C control flow instructions. This will give you an appreciation for how control flow is implemented in assem

String-classes-constructors and member functions, Objective Using C++ s...

Objective Using C++ string, classes, constructors and member functions. Task A Write a program (book.cpp) to store a list of book titles and the published dates. Your pro

What is object variable, The definition of an object(variable ): We can...

The definition of an object(variable ): We can explain a variable(set memory to the variable) in the following ways. e.g. double salary; int month; When more than o

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 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