C++

C++ - Lvalue and Rvalue Reference

Reference Lifetime

Posted by Rico's Nerd Cluster on January 10, 2023

Reference Lifetime

  1. const lvalue reference Simply Extends the Lifetime of A Temp. No copy constructor is called when the temp object is passed in
  2. const lvalue reference as a class attribute DOES NOT EXTEND the lifetime of the temp. It simply stores a reference to it, and it’s dangerous
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class MyClass {
public:
    // Default constructor
    MyClass() {
        std::cout << "Default constructor called\n";
    }
    // Copy constructor
    MyClass(const MyClass&) {
        std::cout << "Copy constructor called\n";
    }
    ~MyClass(){
        std::cout << "MyClass Dtor called\n";
    }
};
void funcByConstRef(const MyClass &obj) {
    std::cout << "Inside funcByConstRef\n";
}
class B{
    public:
    B(){std::cout << "B ctor\n"; }
    void foo(){ std::cout << "B foo\n"; }
    ~B(){ std::cout << "B dtor\n"; }
    const MyClass& m {};
};

int main() {
    // Passing by const reference: 
    funcByConstRef(MyClass());   // No copy is made, copy constructor is NOT called. The temporary object's lifetime is simply extended.
    
    std::cout << "==============================\n";
    B b;    // can see 'MyClass Dtor called' here. So the const lvalue reference is no longer valid!!
    b.foo();
    return 0;
}