Advertisement
tinyevil

Untitled

Jul 29th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. struct Bar{
  2.     private{
  3.         field other:shared_ptr[Bar];
  4.        
  5.         field key_1:i32;
  6.         field key_2:i32;
  7.     }
  8.    
  9.     function new(addr:i32):Bar{
  10.         let seed = random();
  11.         return Bar{key_1 = addr ^ seed, key_2 = seed};
  12.     }
  13.    
  14.     function set_other(self:ref Bar, other:shared_ptr[Bar]){
  15.         self->other = other;
  16.     }
  17.    
  18.     function copy(target:out Bar, source:ref Bar){
  19.         let seed = random();
  20.         target->key_1 = seed;
  21.        
  22.         if ( not source->other.empty() ){
  23.             let other = source->other.get(); //ref Bar
  24.             print(other->key_1 ^ other->key_2); // prints some garbage
  25.         }
  26.        
  27.         target->key_2 = source->key_1 ^ source->key_2 ^ seed;
  28.     }
  29. };
  30.  
  31.  
  32. function foo(){
  33.     var a:shared_ptr[Bar] = shared_ptr::new(Bar::new(1));
  34.     var b:shared_ptr[Bar] = shared_ptr::new(Bar::new(2));
  35.    
  36.     // copies
  37.     external_call(a, b);
  38.    
  39.     *a = *b;
  40. }
  41.  
  42.  
  43. // compiler does not see the source code for this function
  44. function external_call(a b:shared_ptr[Bar]){
  45.     b->set_other(a);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement