Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- // struct Foo {
- // int* i = NULL;
- // Foo() = default;
- // Foo(int i)
- // : i(new int(i))
- // {}
- // Foo(Foo const& other) {
- // if (other.i != NULL) {
- // i = new int(*other.i);
- // }
- // }
- // Foo& operator=(Foo const& other) { //lvalue reference
- // std::cout << "Copy Constructor Called" << std::endl;
- // if (other.i != NULL) {
- // i = new int(*other.i);
- // }
- // return *this;
- // }
- // // Foo& operator=(Foo const& other) {
- // Foo& operator=(Foo&& other) { //rvalue reference
- // std::cout << "Move Called" << std::endl;
- // if (other.i != NULL) {
- // i = other.i;
- // other.i = NULL;
- // }
- // return *this;
- // }
- // ~Foo() {
- // if (i != NULL) {
- // delete i;
- // i = NULL;
- // }
- // }
- // };
- // struct Foo {
- // Foo() = default;
- // Foo(Foo const& other) = default;
- // Foo& operator=(Foo const& other) = default;
- // Foo(Foo&& other) = default;
- // Foo& operator=(Foo&& other) = default;
- // ~Foo() = default;
- // }
- // int returnPRValue() { return 42; }
- // 42 //prvalue
- // int i = 42 //prvalue
- // int i = ReturnPRvalue() //prvalue
- // lvalue -> left value
- // rvalue -> right value
- // prvalue -> pure rvalue
- // xcalue -> eXpring valu
- struct Foo {
- Foo(vector<int>&&v) : v(move(v)) {}
- vector<int> v;
- };
- void fillVector(vector<int>&v) {
- //conditionally fills the vector
- }
- int main() {
- vector<int>vect;
- fillVector(vect);
- //move -> rvalue cast to xvalue
- if (!v.empty()) {
- Foo f1(move(v));
- }
- }
- // int main() {
- // // Foo f1;
- // // {
- // // // Foo f2;
- // // // f2.i = new int(42);
- // // // f1 = std::move(f2); //xvalue
- // // f1 = Foo(42); //prvalue
- // // }
- // }
Add Comment
Please, Sign In to add comment