Guest User

Untitled

a guest
Feb 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct foo {
  4. int a = 5;
  5. int b = 2;
  6. void print() {
  7. std::cout << a + b << std::endl;
  8. }
  9. };
  10.  
  11. template <typename T>
  12. struct bar : T {
  13. void print2() {
  14. std::cout << T::a * T::b << std::endl;
  15. T::b++;
  16. }
  17. };
  18.  
  19. template <typename Base>
  20. bar<Base>* extend_with_bar(Base& base) {
  21. return static_cast<bar<Base>*>(&base);
  22. }
  23.  
  24. int main() {
  25. foo f;
  26.  
  27. f.print();
  28.  
  29. auto b = extend_with_bar(f);
  30.  
  31. b->print2();
  32. b->print2();
  33. b->print();
  34. f.print();
  35. }
Add Comment
Please, Sign In to add comment