I think you've probably got the wrong end of the stick. There's nothing complicated here. Maybe a short sample will help.
include
#include <utility>
class PhysicsState
{
};
using drivingAlgorithm = std::pair<double, double>(PhysicsState ps,
const std::vector<std::pair<double, double>>& goals,
int currentGoal);
class Vehicle
{
public:
Vehicle(drivingAlgorithm da) : _da(da) {}
private:
drivingAlgorithm _da;
};
std::pair<double, double> my_algorithm(PhysicsState ps,
const std::vector<std::pair<double, double>>& goals,
int currentGoal)
{
return std::make_pair(0.0, 0.0);
}
int main()
{
Vehicle v(my_algorithm);
}
using x = ...
just establishes a type alias, not a function alias (such a thing doesn't exist). In this case the type is a function type. But in any case you use the type alias just as you would use any other type.
manpreet
Best Answer
2 years ago
So I am working on an assignment in c++ where we are told to use an alias for a function, or at least for a function pointer (to my understanding). This is not regarded as "syllabus"(what we need to learn, don't know if this is the right word), and therefore has not been lectured.
To give a clearer understanding of the task, I have a class "Vehicle" with a function
draw()
that updates the speed of the vehicle and draws it to the screen. We are then told to use function pointers to move the input-part ofdraw()
to a separate function. This separate function should be a private member of the class, initialized in the constructor. We are then told to use this "alias" to make the code easier to read:This should be placed in a different .h file, where the struct
PhysicsState
is also defined. My question is, How do I use this "alias"? More specifically, where do I define the actual body of the function I am using an alias for? I cant seem to find an answer in our textbook, and neither by searching Google.