小编Dam*_*ola的帖子

将具有可选模板类型参数的函数传递给类构造函数并将其分配给方法

我有两个文件:main.cppcrypto.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++

6
推荐指数
1
解决办法
232
查看次数

标签 统计

c++ ×1