lvalue, rvalue. Copy and Move Constructors

lvalue, rvalue. Copy and Move Constructors

lvalue, rvalue

lvalue(locator value): Anything that can hold a address. Appear on the left-hand side of an assignment. rvalue(read value): temporary value that does not occupy a persistent memory location. Appears on the right-hand side of an assignment


int a = 10;                 // a is l value
                            // 10 is rvalue
int x = a+b;                // x is l value
                            // a+b is rvalue        

lvalue reference (&)

Reference to bind to lvalue. Usage: Pass large objects without copying.

int a = 10;
int &b = a;         // lvalue reference
b = 11;
cout << a;    // 11        

Copy Constructor

Copy Ctr(Creates copy of object) is lvalue reference

using vi = vector <int>;
class A {
    vi a;
    void print() {
        for (decltype(auto)i:a)
            cout << i;
    }
public:
    A(vi& b):a(b) { // copy ctr (using lvalue ref). Created a copy
        cout << "\nCopy ctr\n";
        print();
    }
};
int main() {
    vi a = {1,2,3};
    A obj(a);           // Copy ctr called. Local copy remains present
    cout << "\nAfter copy ctr in main()\n";
    for(decltype(auto)i:a)
        cout << i;
}
/*
$ ./a.out
Copy ctr
1 2 3

After copy ctr in main()
1 2 3
$ 
*/        

rvalue reference (&&)

Reference that binds to an rvalue(temporary value). Usage: ?Move semantics (ie moving a temporary without copying it). ?Perfect forwarding in template functions.

int a = 10;
int &&b = 10;            // rvalue reference        

Move Constructor

Move Ctr(moves the object) is rvalue reference

using vi = vector <int>;
class A {
    vi a;
    void print() {
        for (decltype(auto)i:a)
            cout << i;
    }
public:
    //A(vi&& b):a(b)       ///////// This will make copy. Not correct move ctr ///////////
    A(vi&& b):a(move(b)) {  // move ctr (using rvalue ref).
        cout << "\nmove ctr\n";
        print();
    }
};
int main() {
    vi a = {1,2,3};
    A obj(a);           // Copy ctr called. Local copy remains present

    // move invokes a r value ref
    A obj1(move(a));            // move(a) is equal to (int &&x = a)
    cout << "\nAfter move ctr in main()\n";
    for(decltype(auto)i:a)
        cout << i;
}
/*
$ ./a.out
move ctr
1 2 3

After move ctr in main()    //Value moved not availble in main
$
*/        

XValue / Expiring?Value

(b+c) is called xvalue/temporary value which is ready to expire

a = b+c;          

PRValue / Pure R?Value

Literal value is called pure R value

a = 42;        

Conclusion

lvalue, rvalue provides base for copying and moving objects in C++, hence understanding l,r value reference is important.

要查看或添加评论,请登录

Amit K.的更多文章

  • Pyunit or Unitest

    Pyunit or Unitest

    Used to test a unit of source code Features: 1. PyUnit is very simple yet very flexible 2.

  • Calling OpenAI APIs from code

    Calling OpenAI APIs from code

    Steps a. Get openAI API Key openaAI API Key b.

    3 条评论
  • FlatList with Example in React Native

    FlatList with Example in React Native

    What is FlatList? displays a scrolling list of changing, but similarly structured, data. Unlike the more generic…

  • Create Postgres Database, Tables, Schema using Diesel(ORM) Rust

    Create Postgres Database, Tables, Schema using Diesel(ORM) Rust

    What is Diesel Diesel is a ORM(object-relational mapping). ORM is programming technique that connects object-oriented…

  • Location Sharing App System Design (Bump)

    Location Sharing App System Design (Bump)

    What is Bump Bump is location sharing Mobile App. Install bump on 2 phones(add as friends).

  • Load Balancers & Caches

    Load Balancers & Caches

    What is Load Balancer? Load Balancer evenly distributes incoming traffic/load among webservers/workers that are defined…

  • REST API / Representation State Transfer

    REST API / Representation State Transfer

    Restful Web Server/Application? Web application that implements HTTP CRUD methods in Restful way. Eg: Twitter, facebook…

  • Inter Thread Communication in Rust using Channels

    Inter Thread Communication in Rust using Channels

    What is Channel? Sender and Receiver are connected via Channel. They can send/recv data via channel.

  • Slices in Rust

    Slices in Rust

    What is Slice Slice is somepart of larger data, it always borrow data(Hence RO) from the sliced type. It gives you a…

  • Traits in Rust

    Traits in Rust

    What is Triat in Rust Interface/class in Rust(declared with keyword trait) having Virtual Functions(not pure Virtual)…

社区洞察

其他会员也浏览了