I wrote the following code:
#include <iostream>
using namespace std ;
class C{
public:
C::C(int) ;
int f1(int);
int f2(int);
int (*f)(int);
}
int C::f1(int x){
return -x ;
}
int C::f2(int x){
return x;
}
C::C(int c){
if (c<0){
f = f1 ;
}
else {
f = f2 ;
}
}
This code doesn't work, but the idea is that I want the method f
to be assigned either to f1
or to f2
depending on the value passed to the constructor.
How can I achieve this in C++?