I learned Move Constructors today. I read this answer, and I tried to apply the move constructor example in it to my code.
class UnicodeString
{
public:
enum ENDIANNESS_TYPE {LITTLE_ENDIAN = 0, BIG_ENDIAN = 1} ENDIANNESS;
bool REPLACE_NON_ASCII_CHARACTERS;
char REPLACE_NON_ASCII_CHARACTERS_WITH;
float VECTOR_RESERVE_COEFFICIENT;
UnicodeString(UnicodeString && Other);
// ...
UnicodeString & operator=(UnicodeString Other);
// ...
private:
std::vector<UnicodeChar> UString;
// ...
}
UnicodeString::UnicodeString(UnicodeString && Other)
{
this->REPLACE_NON_ASCII_CHARACTERS = Other.REPLACE_NON_ASCII_CHARACTERS;
this->REPLACE_NON_ASCII_CHARACTERS_WITH = Other.REPLACE_NON_ASCII_CHARACTERS_WITH;
this->VECTOR_RESERVE_COEFFICIENT = Other.VECTOR_RESERVE_COEFFICIENT;
this->ENDIANNESS = Other.ENDIANNESS;
this->UString = ?????
}
UnicodeString & UnicodeString::operator=(UnicodeString Other)
{
std::swap(?????, ?????);
return *this;
}
However, unlike in that example, my class UnicodeString
does not merely contain a simple C array. It contains an std::vector<>
object whose elements are instances of another class I wrote.
First of all, in the move constructor, how do I steal the UString
vector of the other object passed by R-Value?
Secondly, in the assignment operator, how do I efficiently swap references of UString
s of the main UnicodeString
object and the one passed by R-Value? Notice that .UString
is a private property, therefore it cannot be directly accessed from another object