avr39ripe

cppTemplateInheritanceBasics

Aug 12th, 2021
877
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. class BaseA
  5. {
  6. public:
  7.     T valA;
  8. public:
  9.     BaseA() : valA { 1 } { std::cout << "BaseA DEFAULT ctor\n"; }
  10.     BaseA(T valAP) : valA{valAP} { std::cout << "BaseA ctor valA\n"; }
  11.     ~BaseA() { std::cout << "BaseA dtor\n"; }
  12. };
  13.  
  14. template <typename T2>
  15. class Child : public BaseA<T2>
  16. {
  17. protected:
  18.     T2 childVal;
  19. public:
  20.     Child() : BaseA{111}, childVal{ 3 } { std::cout << "Child DEFAULT ctor\n"; }
  21.     Child(T2 childValP) : childVal{ childValP } { std::cout << "ChildA ctor childValP\n"; }
  22.     ~Child() { std::cout << "Child dtor\n"; }
  23. };
  24.  
  25. int main()
  26. {
  27.     Child<int> mChild{ 111 };
  28.  
  29. }
  30.  
RAW Paste Data