Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class A
  5. {
  6. public:
  7.     A(const char *fun_name, int b, int c, int d, int e = 200) :
  8.         fun_name(fun_name),
  9.         b(b),
  10.         c(c),
  11.         d(d),
  12.         e(e)
  13.     {}
  14.  
  15.     ~A()
  16.     {
  17.         cout << "Destructed (called from " << fun_name << ")\n";
  18.     }
  19.  
  20.     const char *fun_name;
  21.     int b;
  22.     int c;
  23.     int d;
  24.     int e;
  25. };
  26.  
  27. int fun1(const A *a)
  28. {
  29.     return a->b + a->c + a->d + a->e;
  30. }
  31.  
  32. int fun2(const A &a)
  33. {
  34.     return a.b + a.c + a.d + a.e;
  35. }
  36.  
  37. int main()
  38. {
  39.     // Usando função com ponteiro
  40.     // cout << fun1(&A(3, 6, 9)) << "\n"; // error: taking address of temporary
  41.  
  42.     A a("fun1", 3, 6, 9);
  43.     cout << fun1(&a) << "\n";
  44.     cout << "fun1 ended\n";
  45.  
  46.     // Usando função com referência constante
  47.     cout << fun2(A("fun2", 3, 6, 9)) << "\n";
  48.     cout << "fun2 ended\n";
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement