Welcome, Guest. Please login or register.

Author Topic: Storm C++ 3.0 and pointer a function  (Read 8916 times)

Description:

0 Members and 1 Guest are viewing this topic.

  • Guest
Storm C++ 3.0 and pointer a function
« 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...
 

Offline DaveP

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2116
    • Show only replies by DaveP
Re: Storm C++ 3.0 and pointer a function
« Reply #1 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.
Hate figure. :lol:
 

Offline Darken

  • Newbie
  • *
  • Join Date: Dec 2002
  • Posts: 8
    • Show only replies by Darken
    • http://perso.wanadoo.fr/psydk
Re: Storm C++ 3.0 and pointer a function
« Reply #2 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);
};
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Storm C++ 3.0 and pointer a function
« Reply #3 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.
int p; // A
 

  • Guest
Re: Storm C++ 3.0 and pointer a function
« Reply #4 on: January 29, 2003, 08:05:22 AM »
Thank you,
is correct!