Search code examples
c++classparametersfunction-parameter

Classes as parameter of function c++


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);

    }

};

Solution

  • 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));