Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int global_a;
  4.  
  5. template<typename D>
  6. struct Base {
  7. int foo() {
  8. if (global_a)
  9. return global_a;
  10. else
  11. return static_cast<D*>(this)->bar();
  12. }
  13.  
  14. int bar() {
  15. return 1;
  16. };
  17. };
  18.  
  19. struct Derived: public Base<Derived> {
  20. int bar() {
  21. return 2;
  22. };
  23. };
  24.  
  25.  
  26. int main (void) {
  27. Derived d;
  28. global_a = 5;
  29. std::cout << "foo() = " << d.foo() << "\n";
  30. global_a = 0;
  31. std::cout << "foo() = " << d.foo() << "\n";
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement