jeudi 28 décembre 2017

Copy constructor called on object instantiation

I may just be miss reading this code but in my attempt to call the default constructor, the code instead calls the copy constructor on my proposed creation.

Could someone give me a knowledge bomb on where my mental model and this code done meet :-/

Below is the code written, its a rough outline for a unique_handler that will hold data by a pointer defined by a trait. This is not relevant but the confusion is in why the auto h = new unique_handler(new My_handler()); performs a copy constructor call instead of a default constructor call.

#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")};

        // ...

    }
}

Aucun commentaire:

Enregistrer un commentaire