As I know,
the purpose of void constructor is to reset all the element from choas state to proper new-born state.
and this statment is quite accruate for me to do it after steal data from rvalue referance.
but it seems to be a invild use for direct call to void constructor,and placement-new not work for this.
so,
- should I call void constructor to reset object in move semantic?
- should I write reset() and invoke() like this one?
- what should void constructor do?
- is my string inplmentation good enough?
#include <cstring>
class STR_imp
{
char* cstr;
static size_t getSize(const char* incstr)
{
return std::strlen(incstr)+1;
};
STR_imp& fillBy(const char* incstr)
{
for(int i=0,size=getSize(incstr);i<size;i++)
this->cstr[i]=incstr[i];
return *this;
};
STR_imp& reset()
{
this->cstr=nullptr;
return *this;
};
STR_imp& invoke()
{
if(this->cstr!=nullptr)
delete[] this->cstr;
return *this;
};
public:
STR_imp():cstr(nullptr)
{};
~STR_imp()
{this->invoke().reset();};
STR_imp(const char* const& incstr);//splited for reading
STR_imp(char*&& incstr):cstr(incstr)
{
incstr=nullptr;
};
STR_imp(const STR_imp& instr);//splited for reading
STR_imp(STR_imp&& instr):cstr(instr.cstr)
{
instr.reset();
};
STR_imp& operator= (const char* const& incstr);//splited for reading
STR_imp& operator= (char*&& incstr)
{
this->invoke();
this->cstr=incstr;
incstr=nullptr;
return *this;
};
STR_imp& operator= (const STR_imp& instr);//splited for reading
STR_imp& operator= (STR_imp&& instr)
{
this->invoke();
this->cstr=instr.cstr;
instr.reset();
return *this;
};
char* operator() ()
{
return this->cstr;
};
};
STR_imp::STR_imp(const char* const& incstr):cstr(new char[getSize(incstr)])
{
this->fillBy(incstr);
};
STR_imp::STR_imp(const STR_imp& instr):cstr(new char[getSize(instr.cstr)])
{
this->fillBy(instr.cstr);
};
STR_imp& STR_imp::operator= (const char* const& incstr)
{
this->invoke();
this->cstr=new char[getSize(incstr)];
this->fillBy(incstr);
return *this;
};
STR_imp& STR_imp::operator= (const STR_imp& instr)
{
this->invoke();
this->cstr=new char[getSize(instr.cstr)];
this->fillBy(instr.cstr);
return *this;
};