Reputation: 11
CursorMove::CursorMove(char& dir)
{
if(dir == 'd')
{
this->execute = CursorMove::moveRight;
}
else if(dir == 'a')
{
this->execute = CursorMove::moveLeft;
}
else if(dir == 'w')
{
this->execute = CursorMove::moveUp;
}
else if(dir == 's')
{
this->execute = CursorMove::moveDown;
}
}
I am currently working on a CS project where we have to be able to move a cursor using the keyboard. The professor wants this to be done via classes because we will be using a while loop to constantly check for user input and then just have a single parent class whose execute will be called. Each unique execute belonging to its own class. I thought it might be easier to try to combine the commands for cursor movement into one. My idea for this was to use public static functions named moveRight, moveLeft, moveUp, and moveDown who all have the same parameters as execute be assigned accordingly to execute during construction. Which one determined by a parameter called "dir".
The error I am currently getting is "reference to non-static member function must be called." Rather than solve the error, I am more curious on whether or not this is a feasible idea or if I am better off just ignoring it and just making a class for each one.
Upvotes: 1
Views: 75
Reputation: 17007
I am more curious on whether or not this is a feasible idea or if I am better off just ignoring it and just making a class for each one.
The overall plan is feasible. It seems more of a C-style approach than C++, but the functionality is supported. How to make it work depends on your class declaration, but you said you were not interested in the "how" (but I will note that the "static" part affects the "how", not the feasibility).
You might want to think about "why", though. What you are doing is basically creating a one-member virtual function table, similar to what the compiler would generate if you declared a virtual function named execute
, then declared derived classes to override that function. The compiler's version has some nice benefits, such as handling the case where dir
is not one of the expected values (possibly via a compile-time error). What are the benefits you hope to obtain with your method?
Upvotes: 2
Reputation: 136
Does your code look something like this? If so, then this is working fine for me:
#include <iostream>
using namespace std;
class CursorMove
{
public:
int (*execute)(void);
static int moveLeft();
static int moveRight();
static int moveUp();
static int moveDown();
CursorMove(char& dir);
};
int CursorMove::moveLeft()
{
cout<<"in move left"<<endl;
return 1;
}
int CursorMove::moveRight()
{
cout<<"in move Right"<<endl;
return 1;
}
int CursorMove::moveUp()
{
cout<<"in move Up"<<endl;
return 1;
}
int CursorMove::moveDown()
{
cout<<"in move Down"<<endl;
return 1;
}
CursorMove::CursorMove(char& dir)
{
if(dir == 'd')
{
this->execute = CursorMove::moveRight;
}
else if(dir == 'a')
{
this->execute = CursorMove::moveLeft;
}
else if(dir == 'w')
{
this->execute = CursorMove::moveUp;
}
else if(dir == 's')
{
this->execute = CursorMove::moveDown;
}
}
int main()
{
char dir = 'a';
CursorMove *obj = new CursorMove(dir);
obj->execute();
return 1;
}
Upvotes: 0