Advertisement
Muzer

Contravariance part II

May 26th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.38 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class A
  4. {
  5.   public:
  6.     int bob;
  7.     A()
  8.     {
  9.       bob = 4;
  10.     }
  11. };
  12.  
  13. class B : public A
  14. {
  15.   public:
  16.     int magic;
  17.     B()
  18.     {
  19.       bob = 5;
  20.       magic = 42;
  21.     }
  22. };
  23.  
  24. void evilFunc(A as[])
  25. {
  26.   A a;
  27.   as[0] = a;
  28. }
  29.  
  30. int main(void)
  31. {
  32.   B bs[1];
  33.   evilFunc(bs);
  34.   std::cout << bs[0].magic << " " << bs[0].bob << std::endl;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement