# Can you have a function pointer as a data member?

Discussion in 'C++' started by Protoman, Jul 4, 2007.

1. ### ProtomanGuest

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: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!!!

Protoman, Jul 4, 2007

2. ### Obnoxious UserGuest

On Wed, 04 Jul 2007 11:29:49 -0700, Protoman wrote:

> 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: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;
}

--
Obnoxious User

Obnoxious User, Jul 4, 2007

3. ### Rolf MagnusGuest

Protoman wrote:

> Can you have a function pointer as a data member?

You can have any type as data member.

Rolf Magnus, Jul 4, 2007
4. ### Victor BazarovGuest

"Rolf Magnus" <> wrote...
> Protoman wrote:
>
> > Can you have a function pointer as a data member?

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

Victor Bazarov, Jul 4, 2007
5. ### Rolf MagnusGuest

Victor Bazarov wrote:

> "Rolf Magnus" <> wrote...
>> Protoman wrote:
>>
>> > Can you have a function pointer as a data member?

>>
>> 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.

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

Rolf Magnus, Jul 4, 2007
6. ### James KanzeGuest

On Jul 4, 10:02 pm, "Victor Bazarov" <> wrote:
> "Rolf Magnus" <> wrote...
> > Protoman wrote:

> > > Can you have a function pointer as a data member?

> > 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.

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:
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

James Kanze, Jul 5, 2007
7. ### ProtomanGuest

On 5 Jul, 02:06, James Kanze <> wrote:
> On Jul 4, 10:02 pm, "Victor Bazarov" <> wrote:
>
> > "Rolf Magnus" <> wrote...
> > > Protoman wrote:
> > > > Can you have a function pointer as a data member?
> > > 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.

>
> 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:
> 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: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?

Protoman, Jul 5, 2007