Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Need help regarding alias for a function

Course Queries Syllabus Queries

Max. 2000 characters
Replies

usr_profile.png

User

( 4 months 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.

usr_profile.png

User

( 4 months 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 <vector>
#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.

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif