Can you have a function pointer as a data member?

P

Protoman

Can you have a function pointer as a data member? Like:

template <class T>
class Integrate
{
public:
Integrate(long double& (T* f)(T& x)):fn(f){}
long double& operator()(T& lowLimit, T& upLimit) const;
private:
long double& (T* fn)(T& x);
};

long double& Integrate::eek:perator()(T& lowLimit, T& upLimit) const
{
long double sum = 0.0;
T x;
// Evaluate integral{a,b} f(x) dx
for (long long n=0;n<=100;n++)
{
x = (n/100.0)*(b-a)+a;
sum +=(*f)(x)*(b-a)/101.0;
}
return sum;
}

Thanks!!!
 
O

Obnoxious User

Can you have a function pointer as a data member? Like:

template <class T>
class Integrate
{
public:
Integrate(long double& (T* f)(T& x)):fn(f){}
long double& operator()(T& lowLimit, T& upLimit) const;
private:
long double& (T* fn)(T& x);
};

long double& Integrate::eek:perator()(T& lowLimit, T& upLimit) const
{
long double sum = 0.0;
T x;
// Evaluate integral{a,b} f(x) dx
for (long long n=0;n<=100;n++)
{
x = (n/100.0)*(b-a)+a;
sum +=(*f)(x)*(b-a)/101.0;
}
return sum;
}

Yes,

#include <iostream>

template <typename ReturnValue, typename Arg1, typename Arg2>
class Function
{
private:
typedef ReturnValue (*FuncPtr)(Arg1,Arg2);
FuncPtr d_fptr;
public:
Function(FuncPtr fptr) : d_fptr(fptr) {}
ReturnValue Call(Arg1 a1, Arg2 a2) {
return d_fptr(a1,a2);
}
};

int plus(int a, int b) {
return a + b;
}

int main() {
Function<int,int,int> Plus(&plus);
std::cout<<Plus.Call(2,1)<<std::endl;
return 0;
}
 
V

Victor Bazarov

Rolf Magnus said:
You can have any type as data member.

... except 'void', of course. Not sure if we consider it a type,
however. Also, an incomplete type is not allowed.

V
 
R

Rolf Magnus

Victor said:
.. except 'void', of course. Not sure if we consider it a type,
however. Also, an incomplete type is not allowed.

When I clicked the send button, I knew there was something I was missing ;-)
Btw: IIRC, void is an incomplete type.
 
J

James Kanze

.. except 'void', of course. Not sure if we consider it a type,
however. Also, an incomplete type is not allowed.

Void is a type. An incomplete type. All members must be
complete.

Also, a data member can't have a function type, otherwise, it
would be a function member:).
 
P

Protoman

Void is a type. An incomplete type. All members must be
complete.

Also, a data member can't have a function type, otherwise, it
would be a function member:).

--
James Kanze (GABI Software) email:[email protected]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oh, and is this an efficient way to solve my problem of integrating an
explicit function in x:

#include <iostream>
using namespace std;

class Integrate
{
public:
Integrate(long double (*f)(long double& x)):fn(f){}
long double operator()(long double& a, long double& b)const;
private:
long double (*fn)(long double& x);
};

long double Integrate::eek:perator()(long double& a, long double&
b)const
{
long double sum=0.0;
// Evaluate integral{a,b} f(x) dx
for (long long n=0;n<=1000000000;n++)
{
long double x = (n/1000000000.0)*(b-a)+a;
sum +=(*fn)(x)*(b-a)/1000000001;
}
return sum;
}

long double square(long double& x);

int main()
{
long double a,b;
cout << "Enter a lower and upper bound: ";
cin >> a >> b;
Integrate integrate(square);
cout << "The integral of x^2 from " << a << " to " << b << " is " <<
integrate(a,b);
return 0;
}

long double square(long double& x)
{
return (x*x);
}

How do you think I could do this at compile-time w/ template
metaprogramming?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top