#Help with function pointers
74 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
!format
void (VehicleList::*getMenuChoice(int choice))() {
void (VehicleList::*options[6])() = {
&VehicleList::addVehicle,
&VehicleList::removeVehicle,
&VehicleList::searchForCar,
&VehicleList::searchForBike,
&VehicleList::sortVehiclesByRegistrationName,
&VehicleList::sortVehiclesByCostPerDay};
return options[choice - 1];
}
void (*getMenuChoice(int choice))();
!format
Nothing to format
yeah you are calling it wrong
void (VehicleList::*getMenuChoice(int choice))() here you're returning a pointer to member function
;compile
#include<iostream>
struct S{
void foo(){
std::cout<<"foo called\n";
}
void bar(){
std::cout<<"bar called\n";
}
};
void(S::*g(int x))(){
void(S::*o[])(){&S::foo,&S::bar};
return o[x];
}
int main(){
S s;
(s.*g(0))();
(s.*g(1))();
}
foo called
bar called
here is a similarly structure function and how to call the returned function
since it is a pointer to member function you need to provide an object to call it on
thankyou 🙂
then calling it
(vehicleList->*vehicleList->getMenuChoice(input))();
!format
I have done it like this now - it works, is it about write?
void (VehicleList::*VehicleList::getMenuChoice(int choice))() {
void (VehicleList::*options[6])() = {
&VehicleList::addVehicle,
&VehicleList::removeVehicle,
&VehicleList::searchForCar,
&VehicleList::searchForBike,
&VehicleList::sortVehiclesByRegistrationName,
&VehicleList::sortVehiclesByCostPerDay};
return options[choice - 1];
}
!format
Nothing to format
(vehicleList->*vehicleList->getMenuChoice(input))();
seems like a strange function but looks like this would work
you don't really use the VehicleList in getMenuChoice
I dont get why I need the exta VehicleList::
in the definition?
void (*VehicleList::getMenuChoice(int choice))() {
void (VehicleList::*options[6])() = {
&VehicleList::addVehicle,
&VehicleList::removeVehicle,
&VehicleList::searchForCar,
&VehicleList::searchForBike,
&VehicleList::sortVehiclesByRegistrationName,
&VehicleList::sortVehiclesByCostPerDay};
return options[choice - 1];
}
(vehicleList->*getMenuChoice(input))();
that means something very different
the return type is a pointer to function
the VehicleList:: here VehicleList::getMenuChoice means to define a method for VehicleList named getMenuChoice
so the whole declaration void (*VehicleList::getMenuChoice(int choice))() means: defining a method named getMenuChoice on the class VehicleList that takes an int named choice and returns a pointer to function taking no arguments and returning nothing
this feels like it would be better as a more or less plain switch case
VehicleList::* means pointer to member of VehicleList, and * means pointer
your choice is just an int, so switch is appropriate
Yeah, but need to use function pointers somewhere in my program
not sure what benefit this list of pointers approach provides?
hmm, is that like part of the assignment?
void (VehicleList::*getMenuChoice(int choice))() {
void (VehicleList::*options[6])() = {
&VehicleList::addVehicle,
&VehicleList::removeVehicle,
&VehicleList::searchForCar,
&VehicleList::searchForBike,
&VehicleList::sortVehiclesByRegistrationName,
&VehicleList::sortVehiclesByCostPerDay};
return options[choice - 1];
}
I should be able to call doing this
(vehicleList->*getMenuChoice(input))();
?
Okay, so obviously this works
!format
void (VehicleList::*VehicleList::getMenuChoice(int choice))() {
void (VehicleList::*options[6])() = {
&VehicleList::addVehicle,
&VehicleList::removeVehicle,
&VehicleList::searchForCar,
&VehicleList::searchForBike,
&VehicleList::sortVehiclesByRegistrationName,
&VehicleList::sortVehiclesByCostPerDay};
return options[choice - 1];
}
and just to be clear, this function return a function pointer to a vehicleList member function that takes no parameters and has a void return type?
in which I call this method doing this
(vehicleList->*vehicleList->getMenuChoice(input))();
Which works, but I am confused as to why I need the second *vehicleList when calling the method
@nocturne urchin Has your question been resolved? If so, run !solved :)
are there any other stipulations about this function pointer you're required to use?
because, like... every function is already a pointer
void foo() {} now foo is a pointer
Nope. Just mentions use function pointers in the application
I will try and figure out a different way to use them 🙂
you created a method on VehicleList to get the pointer to member function
so vehicleList->getMenuChoice(input) gets this pointer to member function, and then you use it by doing vehicleList->*... and then call that function
you could make it something other than a member function since it doesn't use this, a function just named getMenuChoice and do (vehicleList->*getMenuChoice(input))()
Okay, I got you. Thanks for the help kind sir!
This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.