正在调用复制构造函数而不是移动构造函数

时间:2015-01-24 19:13:54

标签: visual-studio-2012 c++11

我试图使用新的C ++ 11移动语义,但每次都会调用复制构造函数...有谁知道我做错了什么?我正在使用VS2012。提前谢谢!

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]);
    // ...
}

0 个答案:

没有答案