I'm trying to use the new C ++ 11 relocation semantics, but the copy constructor is called every time ... Does anyone know what I'm doing wrong? I am using VS2012. Thanks in advance!
MemoryBlock::MemoryBlock(MemoryBlock& other) : m_capacity(other.m_used), m_used(other.m_used), m_pointer(nullptr) { std::wcout << L"Copy constructor called" << std::endl; // ... } MemoryBlock& MemoryBlock::operator=(MemoryBlock& other) { std::wcout << L"Copy assignment called" << std::endl; if (this != &other) { // ... } return *this; } MemoryBlock::MemoryBlock(MemoryBlock&& other) : m_capacity(other.m_capacity), m_used(other.m_used), m_pointer(other.m_pointer) { std::wcout << L"Move constructor called" << std::endl; // ... } MemoryBlock& MemoryBlock::operator=(MemoryBlock&& other) { std::wcout << L"Move assignment called" << std::endl; if (this != &other) { // ... } return *this; } MemoryBlock CreateRequest(const wchar_t *action) { MemoryBlock request; // ... return request; } int __cdecl wmain(int argc, wchar_t *argv[]) { // ... MemoryBlock request = CreateRequest(argv[1]); // ... }
source share