Some good example for using enums

后端 未结 10 1606
感情败类
感情败类 2021-01-05 06:28

I learned enums when I learned C and from time to time I keep myself reminding about it and most of the time by re-reading from some source,it occurred to me that this is d

10条回答
  •  情深已故
    2021-01-05 07:00

    with all said about using enums as symbolic constant, I'd like to emphasize that in C++ using enum in a class gives a very nice, readable and convenient way of encapsulating and exposing class's capabilities, e.g.

    class BlockCipher {
    public:
      enum PaddingOptions { NO_PADDING, DEFAULT_PADDING, ZERO_PADDING /*...*/ }
      void encrypt(const std::string& cleartext, std::string& ciphertext, 
                   PaddingOptions pad=DEFAULT_PADDING);
    };
    
    int main()
    {
      std::string clear("hello, world");
      std::string encrypted;
      BlockCipher  encryptor;
    
      encryptor.encrypt(clear, encrypted, BlockCipher::NO_PADDING);
    }
    

提交回复
热议问题