我有两个文件:main.cpp并crypto.h包含模板类Crypto.在类构造函数中,我需要传递一个函数指针并将其分配给(*keyGen)方法.我需要传递的函数有一个可选的参数
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
}
};
Run Code Online (Sandbox Code Playgroud)
在main.cpp中
int keyGen(int key = -1) { // This …Run Code Online (Sandbox Code Playgroud) c++ ×1