hey,
I´ve been trying to use pointers to functions to alter the effect of a class function and I've apparently been using the syntax incorrectly. I've tried a few different ways and check one ref book but came out empty. If anyone could correct me I'd appreciate it:
as I side note: I was trying to do this because I (baselessly) think that a using a pointer would be more efficient regarding processing speed that having a simple if/else in the workfunc. would this be more efficient?Code:class example2{
private:
double afterswitch(){
cout<<"do something-02"<<endl;
return 0.0;
}
double beforeswitch(){
cout<<"do something else-02"<<endl;
return 0.0;
}
double (*pf)();
public:
example2():pf(beforeswitch){} //error:cannot convert from double (example::)() to double (*)()
double workfunc(){
return pf();
}
void controlfunc(){
pf=afterswitch; //error:cannot convert from double (example::)() to double (*)()
}
};
Code:class example1{
private:
bool control;
public:
example1():control(false){}
double workfunc(){
if (control)
cout<<"do something-01"<<endl;
else
cout<<"do something else-01"<<endl;
return 0.0;
}
void controlfunc(){
control=true;
}
};