Function pointers in classes

  • 18 years ago

    I'm currently working on some real time graphic generations using Direct X and Visual C++ 6.0. I'm famular with general function pointers as shown in this example...


    int add(int rhs, int lhs);
    int sub(int rhs, int lhs);


    int (*math)(int rhs, int lhs);


    void main (void)
    {
       int a = 5, b = 7;
       string temp;
       Thing MyThing;


       cout << "Current a:" << a << " b:" << b << "." << endl;


       cout << "Adding." << endl;
       math = add;
       a = math(a, b);
       cout << "Current a:" << a << " b:" << b << "." << endl;


       cout << "Subtracting." << endl;
       math = sub;
       a = math(a, b);
       cout << "Current a:" << a << " b:" << b << "." << endl;


       cin >> temp;
       return;
    }


    int add(int rhs, int lhs) { return(rhs + lhs);}
    int sub(int rhs, int lhs) { return(rhs - lhs);}


    However, when I create a class, the syntax no longer works, and I am unable to get it to compile. Such as....


    class Thing
    {
    public:
       int add(int rhs, int lhs);
       int sub(int rhs, int lhs);


       int (Thing::*math)(int rhs, int lhs);
    };


    void main (void)
    {
       int a = 5, b = 7;
       string temp;
       Thing MyThing;


       cout << "Current a:" << a << " b:" << b << "." << endl;


       cout << "Adding." << endl;
       MyThing.math = MyThing.add;
       a = MyThing.math(a, b);
       cout << "Current a:" << a << " b:" << b << "." << endl;


       cout << "Subtracting." << endl;
       MyThing.math = MyThing.sub;
       a = MyThing.math(a, b);
       cout << "Current a:" << a << " b:" << b << "." << endl;


       cin >> temp;
       return;
    }


    int Thing::add(int rhs, int lhs) { return(rhs + lhs);}
    int Thing::sub(int rhs, int lhs) { return(rhs - lhs);}



    This code will not compile with an error code saying that MyThing.math never evaluates to a function. What is it that I'm missing? I have noticed that nearlly every C++ book out there seems to just gloss this part over. Any help would be greatly appreciated.


    Grant



  • 18 years ago

    hahaha, i had the same problem, ur gonna kik urself when i tel you...


    god knows why, But u know how u dont need a ";" after the last "}" in a function,


    well you do in a class !!!!


    so as you know a function looks like...


    int main()
    {
      //a load of code
      return 0;
    }


    and a class would look like...


    class CSomeClass
    {
       private:
       //sum private variables and functions
       public:
      //sum public functions
    };



    see,, so slap that little ; in there, and it should work.



  • 18 years ago

    sorry, was looking at the wrong part
    ahh well im an idiot ! sorry.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“It works on my machine.” - Anonymous