类作为函数 c++ 的参数

发布于 2024-11-14 10:30:49 字数 292 浏览 3 评论 0原文

我编写了一堆加密算法作为类,现在我想实现加密模式(维基百科中显示的通用模式,而不是算法规范中的特定模式)。我如何编写一个可以接受任何类的函数?

编辑:

这就是我想要完成的任务

class mode{
  private:
    algorithm_class

  public:
    mode(Algorithm_class, key, mode){
       algorithm_class = Algorithm_class(key, mode);

    }

};

I wrote a bunch of crypto algorithms as classes and now I want to implement encryption modes (generalized modes shown in wikipedia, not the specific ones in the algorithms' specifications). How would I write a function that can accept any of the classes?

edit:

here's what i want to accomplish

class mode{
  private:
    algorithm_class

  public:
    mode(Algorithm_class, key, mode){
       algorithm_class = Algorithm_class(key, mode);

    }

};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青丝拂面 2024-11-21 10:30:49

您可以使用抽象类:

class CryptoAlgorithm
{
   public:
      // whatever functions all the algorithms do
      virtual vector<char> Encrypt(vector<char>)=0;
      virtual vector<char> Decrypt(vector<char>)=0;
      virtual void SetKey(vector<char>)=0;
      // etc
}

// user algorithm
class DES : public CryptoAlgorithm
{
    // implements the Encrypt, Decrypt, SetKey etc
}
// your function
class mode{
public:
    mode(CryptoAlgorithm *algo) // << gets a pointer to an instance of a algo-specific class
           //derived from the abstract interface
         : _algo(algo) {}; // <<- make a "Set" method  to be able to check for null or
                       // throw exceptions, etc
private:
    CryptoAlgorithm *_algo;
}

// in your code
...
_algo->Encrypt(data);
...
//

这样,当您调用 _algo->Encrypt 时 - 您不知道也不关心您正在使用哪种特定算法,只是它实现了接口所有的加密算法都应该被实现。

You can use abstract classes:

class CryptoAlgorithm
{
   public:
      // whatever functions all the algorithms do
      virtual vector<char> Encrypt(vector<char>)=0;
      virtual vector<char> Decrypt(vector<char>)=0;
      virtual void SetKey(vector<char>)=0;
      // etc
}

// user algorithm
class DES : public CryptoAlgorithm
{
    // implements the Encrypt, Decrypt, SetKey etc
}
// your function
class mode{
public:
    mode(CryptoAlgorithm *algo) // << gets a pointer to an instance of a algo-specific class
           //derived from the abstract interface
         : _algo(algo) {}; // <<- make a "Set" method  to be able to check for null or
                       // throw exceptions, etc
private:
    CryptoAlgorithm *_algo;
}

// in your code
...
_algo->Encrypt(data);
...
//

In this way when you call _algo->Encrypt - you don't know and don't care about which specific algorithm you're using, just that it implements the interface all the crypto algorithms should be implementing.

小…红帽 2024-11-21 10:30:49

嗯,怎么样

template<class AlgorithmType>
class mode{
  private:
    AlgorithmType _algo;

  public:
    mode(const AlgorithmType& algo)
      : _algo(algo) {}
};

不需要 modekey 参数,因为算法可以由用户创建:

mode<YourAlgorithm> m(YourAlgorithm(some_key,some_mode));

Well, how about

template<class AlgorithmType>
class mode{
  private:
    AlgorithmType _algo;

  public:
    mode(const AlgorithmType& algo)
      : _algo(algo) {}
};

?

No need for mode and key parameters, as the algorithm can be created by the user:

mode<YourAlgorithm> m(YourAlgorithm(some_key,some_mode));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文