在对象实例化

时间:2017-12-28 16:25:17

标签: c++ c++11 copy-constructor

我可能只是错过了阅读此代码,但在我尝试调用默认构造函数时,代码会在我提议的创建中调用复制构造函数。

有人可以给我一个关于我的心理模型和这个代码完成的地方的知识炸弹: - /

下面是编写的代码,它是通过由特征定义的指针保存数据的unique_handler的粗略轮廓。这是不相关的,但令人困惑的是auto h = new unique_handler(new My_handler());执行复制构造函数调用而不是默认构造函数调用的原因。

#include "../psInclude/my_util.h"

namespace mlekena_smart_class{
    template<typename Traits>
    class unique_handle{

        typedef typename Traits::pointer pointer;

        pointer m_value;
        //...

        public:

        // Delete the copy constructor
        unique_handle(const unique_handle &u_handle) = delete;

        explicit unique_handle(pointer value = Traits::invalid()) throw():
        m_value(value)
        {
        }

        auto get() const throw() -> pointer
        {
            // ...
        }

        auto release() throw() -> pointer
        {
            // ...
        }

        auto reset(pointer value = Traits::invalid) -> bool
        {
            // ...
        }

        ~unique_handle() throw()
        {
            // ..
        }

        explicit operator bool() const throw()
        {
            // ..
        }
    };
    struct null_handle_traits{

        typedef HANDLE pointer;

        // ...
    }

    using namespace mlekena_smart_class;
    using namespace std;

    struct My_handle{
    int id;
    const char* handle;

    My_handle(int _id, const char* h): id(_id), handle(h){}
        ~My_handle(){}
    };

    int main(){

        // offending code
        auto h = null_handle{new My_handle(1, "Handler")};

        // ...

    }
}

0 个答案:

没有答案