fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Pt {
  5. public:
  6. Pt() {
  7. std::cout << "default constructor" << std::endl;
  8. }
  9.  
  10. Pt(const Pt& rvalue) {
  11. std::cout << "copy constructor" << std::endl;
  12. }
  13.  
  14. Pt(const Pt&& rvalue) {
  15. std::cout << "move constructor" << std::endl;
  16. }
  17.  
  18. Pt& operator=(const Pt& rvalue) {
  19. std::cout << "copy assignment" << std::endl;
  20. return *this;
  21. }
  22.  
  23. Pt& operator=(const Pt&& rvalue) {
  24. std::cout << "move assignment" << std::endl;
  25. }
  26.  
  27. ~Pt() {
  28. std::cout << "~Pt" << std::endl;
  29. }
  30.  
  31. //a function to call in order to prevent getting optimized out
  32. void test() {
  33. std::cout << " " << std::endl;
  34. }
  35. };
  36.  
  37. int main() {
  38. {
  39. //here the compiler thinks that we declare a function
  40. std::cout << "step 1" << std::endl;
  41. Pt pt();
  42. }
  43. {
  44. //here the compiler calls a default constructor
  45. std::cout << "step 2" << std::endl;
  46. Pt pt;
  47. }
  48. {
  49. //here the compiler calls a default constructor too
  50. std::cout << "step 3" << std::endl;
  51. Pt pt = Pt();
  52. }
  53.  
  54. {
  55. //here the compiler calls a copy constructor and doesn't call the default constructor prior to that
  56. // O_o
  57. std::cout << "step 4" << std::endl;
  58. Pt pt = pt;
  59. }
  60. return 0;
  61. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
step 1
step 2
default constructor
~Pt
step 3
default constructor
~Pt
step 4
copy constructor
~Pt