Amiga.org

Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: on January 26, 2003, 03:07:40 PM

Title: Storm C++ 3.0 and pointer a function
Post by: on January 26, 2003, 03:07:40 PM
//Hello:
//Problem: pointer a function into class with Storm C 3.0, don't compile!
//I get: expected a ")"


#include
#include


class test{

public:
double f2(int i);
double sum( double (*g)(int m),int n);


};

double test::f2(int i)
{
 return 1.0/(i*i);
}


double test::sum( double (*g)(int m),int n){
   double temp=0.0;
   for (int i=1;i    temp+=(*g)(i);
   return temp;
}




//not in class
double f3(int i)
{
 return 1.0/(i*i*i);
}

//not in class
double sum2( double (*g)(int m),int n){
   double temp=0.0;
   for (int i=1;i    temp+=(*g)(i);
   return temp;
}


int main ()
{
   test* t=new test();
 
   sum2(f3,2);//no error
   
   t->sum(t->f2,2);//error **********  why ???????? ************
   
}

//Please help me! Bye...
Title: Re: Storm C++ 3.0 and pointer a function
Post by: DaveP on January 26, 2003, 03:38:47 PM
Just so you dont feel like no one is looking into this
I just dont have the time this second. If when I get
time I remember to look Ill try to work it out for you
if someone else hasn't already.
Title: Re: Storm C++ 3.0 and pointer a function
Post by: Darken on January 26, 2003, 05:03:02 PM
The class function sum is expecting as first argument a pointer to a global function, not a pointer to a class function.

Here is the version of sum() which receives a pointer to a test class function :

class test
{
public:
  double sum( double ([color=FF9900]test::[/color]*g)(int m),int n);
};
Title: Re: Storm C++ 3.0 and pointer a function
Post by: Karlos on January 28, 2003, 11:21:17 AM
Quote

Darken wrote:
The class function sum is expecting as first argument a pointer to a global function, not a pointer to a class function.

Here is the version of sum() which receives a pointer to a test class function :

class test
{
public:
  double sum( double ([color=FF9900]test::[/color]*g)(int m),int n);
};


Yep!

The thing to remember about function definitions (and hence pointers to functions) is that all non-static class member functions have an invisible argument (the classes' 'this' pointer).

What this means is that

test::sum(double (*g)(int m), int n)

is different from

sum(double (g*)(int m), int n)

and as such have different pointer types.

On the topic of pointers to functions, I'd reccomend that you use a few typedefs to hide the nasty syntax.
Title: Re: Storm C++ 3.0 and pointer a function
Post by: on January 29, 2003, 08:05:22 AM
Thank you,
is correct!