Double Address Operator in C++
Last Updated :
17 Jun, 2024
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;
}
OutputValue 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.
Similar Reads
Arrow Operator vs. Dot Operator in C++ In C++, we use the arrow operator (->) and the dot operator (.) to access members of classes, structures, and unions. Although they sound similar but they are used in different contexts and have distinct behaviours. In this article, we will learn the key differences between the arrow operator and
3 min read
How to Take Operator as Input in C++? Operators are symbols that specify some kind of operation. In C++, we sometimes need to take operators as user input mainly to perform mathematical operations. In this article, we will learn how to take operators as user input in C++. Operators as Input in C++To take operators (like +,-,*,/ etc) as
2 min read
How to Access Elements of a Pair in C++? In C++, a pair container is defined in <utility> header that can store two values that may be of different data types so as to store two heterogeneous objects as a single unit. In this article, we will learn how to access elements of a pair in C++. Example:Input: myPair={10,G} Output: First El
2 min read
Add Two Numbers in C++ Given two integers, the task is to add these integer number and print their sum in C++.ExamplesInput: a = 11, b = 9Output: 20Explanation: Sum of 11 + 9 = 20Input: a = 1, b = 8Output: 9Explanation: Sum of 1 + 8 = 9 Add Two Numbers Using Addition OperatorIn C++, the simplest method for adding the two
3 min read
How to Round a Double to an int in C++? In C++, the double data type is used to store floating-point numbers with double precision. In this article, we will learn how to round a double value to an int in C++. Example: Input: double val= 2.6 Output: Integer for double value 2.5 is: 3 Rounding a Double to an Int in C++To round a double valu
2 min read