Are the move and copy constructors only called when creating an object? if yes, after the creation of an object, it is not possible to invoke either the move or copy constructor of that object, right? The doubt came from here:
class Engine
{
public:
Engine(int length) : m_size(length), m_ptr(new int[length])
{
}
~Engine()
{
delete[] m_ptr;
}
Engine(const Engine& e) : m_ptr(new int[e.m_size])
{
this->m_size = e.m_size;
*this->m_ptr = *e.m_ptr;
}
private:
int* m_ptr;
int m_size;
};
As we can see, when the constructor is called, it allocates space in the heap pro m_ptr
. So if the copy constructor was somehow called after that,
it would allocate memory again in m_ptr
and the previously allocated memory would be leaked.
I'm almost sure of the answer, but since I've never seen any explicit mention of it, I leave my question here.