#Help with function pointers

74 messages · Page 1 of 1 (latest)

nocturne urchin
#

Hi, I have a menu system on my application and trying to implement function pointers to do this. I am getting error on this code: Where am I going wrong?

woeful nimbusBOT
#

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.

nocturne urchin
#

!format

woeful nimbusBOT
#
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];
}
JAMMEISTER
nocturne urchin
#

in the header -

#

!format

woeful nimbusBOT
#
void (*getMenuChoice(int choice))();
JAMMEISTER
nocturne urchin
#

and calling it by doing this -

#

getMenuChoice(input)();

nocturne urchin
woeful nimbusBOT
nocturne urchin
#

Where am I going wrong?

#

I think i am calling it wrong

#

Not a clue

vital briar
#

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))();
}
idle jayBOT
#
Program Output
foo called
bar called
vital briar
#

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

nocturne urchin
#

thankyou 🙂

#

then calling it

(vehicleList->*vehicleList->getMenuChoice(input))();

#

!format

woeful nimbusBOT
#

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];
}
JAMMEISTER
woeful nimbusBOT
nocturne urchin
vital briar
#

you don't really use the VehicleList in getMenuChoice

nocturne urchin
#

I dont get why I need the exta VehicleList::

vital briar
#

in the definition?

nocturne urchin
#

Because If i remove it

#

hang on

#

1 sec

#

!format

woeful nimbusBOT
#
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];
}
JAMMEISTER
nocturne urchin
#

(vehicleList->*getMenuChoice(input))();

vital briar
#

that means something very different

nocturne urchin
#

would you mind explaining?

#

sorry to be a pain

vital briar
#

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

uncut marlin
#

this feels like it would be better as a more or less plain switch case

vital briar
#

VehicleList::* means pointer to member of VehicleList, and * means pointer

uncut marlin
#

your choice is just an int, so switch is appropriate

nocturne urchin
#

Yeah, but need to use function pointers somewhere in my program

uncut marlin
#

not sure what benefit this list of pointers approach provides?

uncut marlin
nocturne urchin
#

yeah

#

This is literally the only use case I can think of

#

so this

#

!format

woeful nimbusBOT
#
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];
}
JAMMEISTER
nocturne urchin
#

I should be able to call doing this

#

(vehicleList->*getMenuChoice(input))();

#

?

#

Okay, so obviously this works

#

!format

woeful nimbusBOT
#
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];
}
JAMMEISTER
nocturne urchin
#

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

woeful nimbusBOT
#

@nocturne urchin Has your question been resolved? If so, run !solved :)

uncut marlin
#

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

nocturne urchin
#

Nope. Just mentions use function pointers in the application

#

I will try and figure out a different way to use them 🙂

vital briar
#

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))()

nocturne urchin
#

Okay, I got you. Thanks for the help kind sir!

woeful nimbusBOT
#

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.