An lvalue (locator value) represents an object that occupies some identifiable location in memory (i.e. has an address).
rvalues are defined by exclusion, by saying that every expression is either an lvalue or an rvalue. Therefore, from the above definition of lvalue, an rvalue is an expression that does not represent an object occupying some identifiable location in memory.
other.m_Size = 0; other.m_Data = nullptr; } return *this; } // Destructor to release dynamically allocated memory ~String() { delete[] m_Data; printf("Destroyed!\n"); } // Method to print the contents of the String object voidPrint(){ for (uint32_t i = 0; i < m_Size; i++) { printf("%c", m_Data[i]); } printf("\n"); }
private: char* m_Data; // Pointer to the string data uint32_t m_Size; // Size of the string };
classEntity { private: String m_Name; // A member variable of type String public: // Constructor accepting a lvalue reference, used to initialize Entity object Entity(const String& name) : m_Name(name) { } // Constructor accepting a rvalue reference, used to initialize Entity object Entity(String&& name) : m_Name(std::move(name)) { } // Method to print the name of the Entity voidPrintName(){ m_Name.Print(); } };
intmain(){ // Creating an Entity object with a string literal (lvalue) printf("--------------------------------------\n"); Entity e("Randolfluo"); e.PrintName(); printf("--------------------------------------\n"); // Creating a String object with a string literal String arr = "Ran"; // Creating an Entity object with a named String object (lvalue) Entity e1(arr); e1.PrintName();
printf("--------------------------------------\n"); // Creating two String objects String arr1 = "Apple"; String arr2; arr1.Print(); arr2.Print(); printf("--------------------------------------\n"); // Moving the content of one String object to another arr2 = std::move(arr1); arr1.Print(); arr2.Print(); printf("--------------------------------------\n"); std::cin.get(); return0; } /* -------------------------------------- Created! Moved! Destroyed! Randolfluo -------------------------------------- Created! Copied! Ran -------------------------------------- Created! Apple -------------------------------------- Moved! Apple --------------------------------------*/