Wednesday, 2 December 2015

Session 11

Operations!
One example of an operation is x == y. This is the equality operation
It compares one thing to another (x is equal to y), if it was a single = it'd be assigning the value instead of comparing

Function pointers
A function identifier is a constant pointer to a function, eg:
void run(); //the brackets are the dereference bit

The address of a function can be stored, eg:
int(*anyName)(Health&);
anyName(gremlin.getHealth());

The brackets specify that you want the function's address and not just a generic pointer address
Storing pointer functions - implicit means you don't specify that the pointer is a function pointer, it is only implied.
Explicit function pointer storing requires that you Do specify (using (*x), where x is the name of the function)
It is possible to pass function pointers to different functions
But why? You can compare, by adding the function pointer to the function signature

Decltype evaluates type of an expression to cheat our declaration of function pointers as a time
Decltype is like the auto keyword; saves time and effort (looks a bit neater too for longer declarations)
Auto saves having to write: bool (*functionPointer)(int x, int y)
Can use function pointers when making a factory

Static GameObject* createObject() {return new PlayingCard;}
is the same as:
std::map<std::string, GameObject*(*)() map;

State Machine - if all action-handling functions have the same signature, you can use function pointers for the input
Functor - a function 'wrapped up' as a class. Can use, for example, a multiplication class as if it were a function

No comments:

Post a Comment