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
Enter your message below
Sign in or Join us (it's free).