Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC6
Hi All
 
I have to face problem in classes by using pointers function.
for example
 
header file
 
testPtr.h
 
class testPtr
{
    float *add(float *a,int n)
};
 

in main the use of this class
 
   //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 29-Apr-12 22:03pm
prog786374
Edited 29-Apr-12 22:05pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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;
 

  }
  Permalink  
Comments
Sandeep Mewara at 3-May-12 8:56am
   
Neat. 5ed.
Espen Harlinn at 3-May-12 8:57am
   
Thank you, Sandeep :-D
SAKryukov at 5-May-12 22:22pm
   
Good, my 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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

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



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 30 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100