Need help regarding alias for a function

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer 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 of draw() 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:

using drivingAlgorithm = std::pair<double,double> ( PhysicsState ps,
const std::vector<std::pair<double,double>>& goals,
int currentGoal);

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.

profilepic.png
manpreet 2 years ago

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.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.