I have two files: main.cpp
and crypto.h
containing the template class Crypto. In the class constructor I need to pass a function pointer and assign it to the (*keyGen) method. The function I need to pass has an optional param
template <class keyType>
class Crypto {
private:
keyType (*keyGen)(keyType);
public:
Crypto(keyType (*keyGen)(keyType)) {
this->keyGen = keyGen;
}
void decode() {
keyType foundKey;
vector<string> output;
keyType curKey = keyGen(); // Here keyGen has no args
// if curKey does not decode the input
curKey = keyGen(curKey); // Here keyGen has 1 arg of type keyType
// else foundKey = curKey;
// Save the decoded file
}
};
In main.cpp
int keyGen(int key = -1) { // This is the optional param
key++;
return key;
}
int main() {
// ...
Crypto<int> crypto(keyGen);
crypto.decode();
}
I need the decode method to be able to call keyGen with both no params or a keyType param. If keyGen is called with no params I need to return 0, else I need to return key+1. I thought about overloading the keyGen function, but since it is a function pointer it is not possible. I did a lot of research but I didn't find a solution.