C++11 移动构造函数和std::move演示

2022-02-08  本文已影响0人  FredricZhu

非常基础的指针资源管理。

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

if(APPLE)
    message(STATUS "This is Apple, do nothing.")
elseif(UNIX)
    message(STATUS "This is linux, set CMAKE_PREFIX_PATH.")
    set(CMAKE_PREFIX_PATH /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/share)
endif(APPLE)

project(copy_construct)

add_definitions(-std=c++17)

add_definitions(-g)

find_package(ZLIB)

find_package(glog REQUIRED)

find_package(OpenCV REQUIRED )

find_package(Boost REQUIRED COMPONENTS
    system
    filesystem
    serialization
    program_options
    thread
    )

find_package(DataFrame REQUIRED)

if(APPLE)
    MESSAGE(STATUS "This is APPLE, set INCLUDE_DIRS")
set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include /usr/local/iODBC/include /opt/snowflake/snowflakeodbc/include/ ${CMAKE_CURRENT_SOURCE_DIR}/../../include/ ${CMAKE_CURRENT_SOURCE_DIR}/include/)
elseif(UNIX)
    MESSAGE(STATUS "This is linux, set INCLUDE_DIRS")
    set(INCLUDE_DIRS ${Boost_INCLUDE_DIRS} /usr/local/include ${CMAKE_CURRENT_SOURCE_DIR}/../../include/ ${CMAKE_CURRENT_SOURCE_DIR}/include/)
endif(APPLE)


if(APPLE)
    MESSAGE(STATUS "This is APPLE, set LINK_DIRS")
    set(LINK_DIRS /usr/local/lib /usr/local/iODBC/lib /opt/snowflake/snowflakeodbc/lib/universal)
elseif(UNIX)
    MESSAGE(STATUS "This is linux, set LINK_DIRS")
    set(LINK_DIRS ${Boost_INCLUDE_DIRS} /usr/local/lib /vcpkg/ports/cppwork/vcpkg_installed/x64-linux/lib)
endif(APPLE)

if(APPLE)
    MESSAGE(STATUS "This is APPLE, set ODBC_LIBS")
    set(ODBC_LIBS iodbc iodbcinst)
elseif(UNIX)
    MESSAGE(STATUS "This is linux, set LINK_DIRS")
    set(ODBC_LIBS odbc odbcinst ltdl)
endif(APPLE)

include_directories(${INCLUDE_DIRS})
LINK_DIRECTORIES(${LINK_DIRS})

file( GLOB main_file_list main.cpp) 

file( GLOB APP_SOURCES  ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/http/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/yaml/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/df/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/death_handler/impl/*.cpp  ${CMAKE_CURRENT_SOURCE_DIR}/include/utils/*.h ${CMAKE_CURRENT_SOURCE_DIR}/include/utils/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/impl/utils/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/impl/beans/*.cpp)

add_library(${PROJECT_NAME}_lib SHARED ${APP_SOURCES})
target_link_libraries(${PROJECT_NAME}_lib ${Boost_LIBRARIES} ZLIB::ZLIB glog::glog DataFrame::DataFrame ${OpenCV_LIBS})
target_link_libraries(${PROJECT_NAME}_lib  ssl crypto libgtest.a pystring libyaml-cpp.a libgmock.a ${ODBC_LIBS} libnanodbc.a pthread dl backtrace)

foreach( main_file ${main_file_list} )
    file(RELATIVE_PATH filename ${CMAKE_CURRENT_SOURCE_DIR} ${main_file})
    string(REPLACE ".cpp" "" file ${filename})
    add_executable(${file}  ${main_file})
    target_link_libraries(${file} ${PROJECT_NAME}_lib)
endforeach( main_file ${main_file_list})

integer.h

#ifndef _FREDRIC_INTEGER_H_
#define _FREDRIC_INTEGER_H_

class Integer {
    int *m_pInt;

    public:
        Integer();
        Integer(int value);
        int get_value() const;
        void set_value(int value);

        Integer(Integer const& rhs);
        Integer& operator=(Integer const& rhs);
        Integer(Integer&& rhs);
        Integer& operator=(Integer&& rhs);
        ~Integer();
};

#endif

integer.cpp

#include "integer.h"

Integer::Integer() {
    m_pInt = new int(0);
}

Integer::Integer(int value) {
    m_pInt = new int(value);
}

int Integer::get_value() const {
    return *m_pInt;
}

void Integer::set_value(int value) {
    *m_pInt = value;
}

Integer::Integer(Integer const& rhs) {
    m_pInt = new int(rhs.get_value());
}

Integer& Integer::operator=(Integer const& rhs) {
    if(this == &rhs) {
        return *this;
    }
    if(m_pInt != nullptr) {
        delete m_pInt;
    }

    m_pInt = new int(rhs.get_value());
    return *this;
}

Integer::Integer(Integer&& rhs) {
    m_pInt = rhs.m_pInt;
    rhs.m_pInt = nullptr;
}

Integer& Integer::operator=(Integer&& rhs) {
    if(this == &rhs) {
        return *this;
    }
    m_pInt = rhs.m_pInt;
    rhs.m_pInt = nullptr;
    return *this;
}

Integer::~Integer() {
    delete m_pInt;
    m_pInt = nullptr;
}

main.cpp

#include "integer.h"

#include <iostream>

int main(int argc, char* argv[]) {
    Integer i1 {1};
    Integer i2 {i1};

    std::cout << i1.get_value() << std::endl;
    std::cout << i2.get_value() << std::endl;

    i2.set_value(2);
    std::cout << i1.get_value() << std::endl;
    std::cout << i2.get_value() << std::endl;

    Integer i3 = std::move(i1);
    // move 之后变成 null_ptr,再解引用会崩。
    // std::cout << i1.get_value() << std::endl;
    std::cout << i2.get_value() << std::endl;
    std::cout << i3.get_value() << std::endl;
    return EXIT_SUCCESS;
}

程序输出如下


image.png
上一篇下一篇

猜你喜欢

热点阅读