Open In App

Double Address Operator in C++

Last Updated : 17 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, the double address operator (&&) is used to denote an rvalue reference, which is a type of reference that can bind to temporary objects. In this article, we will learn how to use the double address operator in C++.

Double Reference Operator in C++

To understand the double address operator, we first need to understand the concept of rvalues and lvalues.

  • Lvalues are expressions that refer to a memory location and whose address can be retrieved. Variables in C++ are lvalues.
  • Rvalues are temporary objects or literals that are used during computation but do not persist beyond their immediate use.

An rvalue reference (&&) allows us to bind to rvalues, facilitating move semantics and perfect forwarding.

The && operator is used to create rvalue references, which are a type of reference that can only bind to temporary objects. https://origin.geeksforgeeks.org/how-to-create-a-dynamic-library-in-c/

Example of Double Address Operator && in C++

The below example demonstrates the use of the double address operator in C++.

C++
#include <iostream>
#include <utility> // For std::move

class DynamicArray {
private:
    int* data;
    size_t size;

public:
    // Constructor
    DynamicArray(size_t s)
        : size(s)
        , data(new int[s])
    {
        std::cout << "Constructor: Allocated " << size
                  << " integers.\n";
    }

    // Destructor
    ~DynamicArray()
    {
        delete[] data;
        std::cout << "Destructor: Released memory.\n";
    }

    // Copy Constructor (Disabled)
    DynamicArray(const DynamicArray& other) = delete;

    // Copy Assignment Operator (Disabled)
    DynamicArray& operator=(const DynamicArray& other)
        = delete;

    // Move Constructor
    DynamicArray(DynamicArray&& other) noexcept
        : data(other.data),
          size(other.size)
    {
        other.data = nullptr;
        other.size = 0;
        std::cout << "Move Constructor: Moved ownership of "
                     "data.\n";
    }

    // Move Assignment Operator
    DynamicArray& operator=(DynamicArray&& other) noexcept
    {
        if (this != &other) {
            delete[] data; // Release old memory

            data = other.data; // Transfer ownership
            size = other.size;

            other.data = nullptr; // Nullify the source
            other.size = 0;
            std::cout << "Move Assignment: Moved ownership "
                         "of data.\n";
        }
        return *this;
    }

    // Print function to display the array size
    void print() const
    {
        std::cout << "Array size: " << size << "\n";
    }
};

int main()
{
    // Create a DynamicArray
    DynamicArray arr1(10);
    arr1.print();

    // Move arr1 to arr2 using the move constructor
    DynamicArray arr2(std::move(arr1));
    arr2.print();
    arr1.print(); // arr1 is now in a null state

    // Create another DynamicArray
    DynamicArray arr3(20);
    arr3.print();

    // Move arr3 to arr2 using the move assignment operator
    arr2 = std::move(arr3);
    arr2.print();
    arr3.print(); // arr3 is now in a null state

    return 0;
}

Output
Value of rvalue is: 10

Perfect Forwarding in C++

Perfect forwarding refers to the ability to pass arguments to a function while preserving their value category (lvalue or rvalue). This is particularly useful in template programming.



Next Article
Article Tags :
Practice Tags :

Similar Reads