Can you have a function pointer as a data member?

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

  1. Protoman

    Protoman Guest

    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!!!
     
    Protoman, Jul 4, 2007
    #1
    1. Advertising

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

    --
    Obnoxious User
     
    Obnoxious User, Jul 4, 2007
    #2
    1. Advertising

  3. Protoman

    Rolf Magnus Guest

    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
    #3
  4. "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
    --
    Please remove capital A's from my address when replying by mail
     
    Victor Bazarov, Jul 4, 2007
    #4
  5. Protoman

    Rolf Magnus Guest

    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
    #5
  6. Protoman

    James Kanze Guest

    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
    #6
  7. Protoman

    Protoman Guest

    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::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?
     
    Protoman, Jul 5, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    5
    Views:
    630
    John Carson
    Jul 30, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,068
    Fraser Ross
    Aug 14, 2004
  3. Stephen Howe
    Replies:
    2
    Views:
    298
    Stephen Howe
    Nov 6, 2012
  4. somenath
    Replies:
    10
    Views:
    293
    James Kanze
    Jul 2, 2013
  5. somenath
    Replies:
    2
    Views:
    170
    somenath
    Aug 29, 2013
Loading...

Share This Page