ZaMaZaN4iK

C++ example

Jan 18th, 2021
927
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <memory>
  2. #include <iostream>
  3. #include <cassert>
  4. #include <vector>
  5. #include <utility>
  6.  
  7. class ITransport
  8. {
  9. public:
  10.     virtual ~ITransport() = default;
  11. };
  12.  
  13. using ITransportPtr = std::unique_ptr<ITransport>;
  14.  
  15. class NetworkTransport : public ITransport
  16. {
  17. public:
  18.     ~NetworkTransport() override = default;
  19. };
  20.  
  21. class SerialTransport : public ITransport
  22. {
  23. public:
  24.     ~SerialTransport() override = default;
  25. };
  26.  
  27. class A
  28. {
  29. public:
  30.     A()
  31.     {
  32.         add_transport<NetworkTransport>();
  33.         add_transport<SerialTransport>();
  34.     }
  35.    
  36.     int transport_count() { return m_transports.size(); }
  37.    
  38. private:
  39.     template <typename T>
  40.     void add_transport()
  41.     {
  42.         auto tr = std::make_unique<T>();
  43.         m_transports.push_back(std::move(tr));
  44.     }
  45.    
  46. private:
  47.     std::vector<ITransportPtr> m_transports;
  48. };
  49.  
  50. int main()
  51. {
  52.     A a;
  53.     std::cout << a.transport_count() << std::endl;
  54. }
RAW Paste Data