Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All

I have to face problem in classes by using pointers function.
for example

header file

testPtr.h

C++
class testPtr
{
    float *add(float *a,int n)
};



in main the use of this class

C++
   //testing.cpp 

// object of class
testPtr testPtrObject();

void main 
{

testPtrObject.add(a,n);

}

 
float testPtr::*add(float a int n)

{

----------
-------
}


i ve to face the problem only in case of pointer
other wise if i use simple functions in header file and then call these function by creating the objects of that class in main class i can easily access that functions and use them any whaere i neede by creating the object of that class

kindly guide me what to do in case of pointer function
Posted
Updated 29-Apr-12 21:05pm
v2

I assume you have a problem calling a member function via a pointer. That is not as simple as calling a simple (non-class) function, because two things are involved: (a) the function itself (b) the object that the member function should be called on.

For your example, a member function call would look like:
typedef float* (testPtr::*MemFuncPtr) (float *a, int n);
MemFuncPtr p = &testPtr::add;

// calling sequence:
(testPtrObject.*p) (&a, n);


As you see, the syntax is awkward and one of the weaker points of C++.

Instead of handling the pointer to function and the object pointer separately, why not bundle them together into a single object. That's what is generally called a delegate.

You find a lot of good articles here on CodeProject about delegates, for example this one:

Member Function Pointers and the Fastest Possible C++ Delegates

Hope that gets you on track.
 
Share this answer
 
When it's declared like his:
class testPtr
{
public:
    float *add(float *a,int n);
};


The implementation should look like this:
float* testPtr::add(float* a int n)
{
//----------
//-------
}


It can then be used like this:
int main()
{
  testPtr testPtrObject;
  float b = 1.0;
  float* a =&b;
  int n = 2; 
  float* c = testPtrObject.add(a,n);

  testPtr* testPtrObjectPtr = new testPtr();
  float* c = testPtrObjectPtr->add(a,n);
  delete testPtrObjectPtr;


  }
 
Share this answer
 
Comments
Sandeep Mewara 3-May-12 8:56am    
Neat. 5ed.
Espen Harlinn 3-May-12 8:57am    
Thank you, Sandeep :-D
Sergey Alexandrovich Kryukov 5-May-12 22:22pm    
Good, my 5.
--SA
class testPtr
{
float *add(float *a,int n)
};



inspite of creating objectthis way testPtr testPtrObject();
create object like this testPtr testPtrObject


void main
{

testPtrObject.add(a,n);

}





also here the definition that you have written is not appropriate

float testPtr::*add(float a int n)

{

----------
-------
}


change this like as float* testPtr::add(float* a, int n)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900