Guest User

Untitled

a guest
Dec 11th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. /* 編譯指令:g++ test.cpp -fno-elide-constructors
  2. /* 執行結果:
  3. james732@james:~$ ./a.out
  4. default constructor
  5. copy constructor
  6. copy constructor
  7. ---------------
  8. default constructor
  9. copy constructor
  10. copy assignment
  11. default constructor
  12. copy constructor
  13. */
  14.  
  15. #include <iostream>
  16. using namespace std;
  17.  
  18. class Foo {
  19. public:
  20. Foo() {
  21. cout << "default constructor" << endl;
  22. }
  23. Foo( const Foo & rh ) {
  24. cout << "copy constructor" << endl;
  25. }
  26. Foo operator=( const Foo & rh ) {
  27. cout << "copy assignment" << endl;
  28. Foo f;
  29. return f;
  30. }
  31. };
  32.  
  33. Foo do_something ( ) {
  34. Foo f1;
  35. return f1;
  36. }
  37.  
  38. int main()
  39. {
  40. Foo f3 = do_something( );
  41. cout << "---------------" << endl;
  42. f3 = do_something( ); /* 這行才會引發 assignment */
  43. }
Add Comment
Please, Sign In to add comment