0

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 UStrings 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.

Community
  • 1
  • 1
hkBattousai
  • 10,583
  • 18
  • 76
  • 124
  • 1
    `UnicodeString`s should be able to see each others `UString`s not matter if the are `private` or not. The access specifier applies to other customers. – Benjamin Bannier May 09 '13 at 14:26

2 Answers2

3

First of all, in the move constructor, how do I steal the UString vector of the other object passed by R-Value?

Just move() the std::vector as it is moveable (operator=(vector&&)):

this->UString = std::move(Other.UString);

Secondly, in the assignment operator, how do I efficiently swap references of UStrings 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.

Note that private applies to the class, not to an instance of the class. Meaning other instances of the same class can access the private members of another instance. So, just use std::move(Other.UString) as before.

hmjd
  • 120,187
  • 20
  • 207
  • 252
2

The correct way to do this (assuming that you're using Visual Studio and therefore your compiler won't do it for you) is like this, with an initialization list:

Unicode::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(std::move(other.UString))
{
}

Notice that UString is a private property, therefore it cannot be directly accessed from another object.

Says who? Private means that code outside of the object can't access it. But one object instance most certainly can access the privates of another object instance.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982