jeudi 27 décembre 2018

Calling constructor from copy constructor

From c++ 11 we can call a constructor from another constructor. So instead of defining copy constructor can we call the constructor every time? Like in this piece of code :

class MyString
{
private:
    char *ptr;
    int m_length;
public:
    MyString(const char *parm = nullptr) : m_length(0), ptr(nullptr)
    {
        if (parm)
        {
            m_length = strlen(parm) + 1;
            ptr = new char[m_length];
            memcpy(ptr, parm, m_length);
        }
    }
    MyString(const MyString &parm) : MyString(parm.ptr)
    {

    }
};

Is there any ill effect to this approach? Is there any advantage of writing traditional copy constructor?

Aucun commentaire:

Enregistrer un commentaire