Guest User

Reference a dereference

a guest
Mar 6th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. struct BOD
  2. {
  3.     int x;
  4.     int y;
  5. };
  6.  
  7. BOD *MyFunc1(BOD *val1)
  8. {
  9.     return val1;
  10. };
  11.  
  12. BOD &MyFunc2(BOD &val1)
  13. {
  14.     return val1;
  15. };
  16.  
  17. BOD MyFunc3(BOD &val1)
  18. {
  19.     return val1;
  20. };
  21.  
  22. int _tmain(int argc, _TCHAR* argv[])
  23. {
  24.     // *** DYNAMICKÁ DEKLARACE *** //
  25.     BOD *b1 = new BOD;
  26.     b1->x = 5;
  27.  
  28.     BOD *bod1 = MyFunc1(b1); // Vypíše 5
  29.     cout << bod1->x << endl;
  30.  
  31.     BOD &bod2 = MyFunc2(*b1); // Vypíše 5
  32.     cout << bod2.x << endl;
  33.  
  34.     BOD bod3 = MyFunc3(*b1); // Vypíše 5
  35.     cout << bod3.x << endl;
  36.    
  37.     // *** STATICKÁ DEKLARACE *** //
  38.     BOD b2;
  39.     b2.x = 6;
  40.  
  41.     BOD *bod4 = MyFunc1(&b2); // Vypíše 6
  42.     cout << bod4->x << endl;
  43.    
  44.     BOD &bod5 = MyFunc2(b2); // Vypíše 6
  45.     cout << bod5.x << endl;
  46.    
  47.     BOD bod6 = MyFunc3(b2); // Vypíše 6
  48.     cout << bod6.x << endl;
  49.    
  50.     system("PAUSE");
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment