Guest User

Untitled

a guest
Feb 17th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. // struct Foo {
  5. // int* i = NULL;
  6. // Foo() = default;
  7. // Foo(int i)
  8. // : i(new int(i))
  9. // {}
  10. // Foo(Foo const& other) {
  11. // if (other.i != NULL) {
  12. // i = new int(*other.i);
  13. // }
  14. // }
  15. // Foo& operator=(Foo const& other) { //lvalue reference
  16. // std::cout << "Copy Constructor Called" << std::endl;
  17. // if (other.i != NULL) {
  18. // i = new int(*other.i);
  19. // }
  20. // return *this;
  21. // }
  22. // // Foo& operator=(Foo const& other) {
  23. // Foo& operator=(Foo&& other) { //rvalue reference
  24. // std::cout << "Move Called" << std::endl;
  25. // if (other.i != NULL) {
  26. // i = other.i;
  27. // other.i = NULL;
  28. // }
  29. // return *this;
  30. // }
  31.  
  32. // ~Foo() {
  33. // if (i != NULL) {
  34. // delete i;
  35. // i = NULL;
  36. // }
  37. // }
  38. // };
  39.  
  40. // struct Foo {
  41. // Foo() = default;
  42. // Foo(Foo const& other) = default;
  43. // Foo& operator=(Foo const& other) = default;
  44. // Foo(Foo&& other) = default;
  45. // Foo& operator=(Foo&& other) = default;
  46. // ~Foo() = default;
  47. // }
  48.  
  49.  
  50.  
  51. // int returnPRValue() { return 42; }
  52.  
  53. // 42 //prvalue
  54. // int i = 42 //prvalue
  55. // int i = ReturnPRvalue() //prvalue
  56. // lvalue -> left value
  57. // rvalue -> right value
  58. // prvalue -> pure rvalue
  59. // xcalue -> eXpring valu
  60.  
  61. struct Foo {
  62. Foo(vector<int>&&v) : v(move(v)) {}
  63. vector<int> v;
  64. };
  65.  
  66. void fillVector(vector<int>&v) {
  67. //conditionally fills the vector
  68. }
  69.  
  70. int main() {
  71. vector<int>vect;
  72. fillVector(vect);
  73. //move -> rvalue cast to xvalue
  74. if (!v.empty()) {
  75. Foo f1(move(v));
  76. }
  77. }
  78.  
  79. // int main() {
  80.  
  81. // // Foo f1;
  82. // // {
  83. // // // Foo f2;
  84.  
  85. // // // f2.i = new int(42);
  86. // // // f1 = std::move(f2); //xvalue
  87. // // f1 = Foo(42); //prvalue
  88. // // }
  89. // }
Add Comment
Please, Sign In to add comment