Default eight functions for C + + classes
1#include <iostream>2#include <assert.h>3 4 classMyClass5 {6 Public:7MyClass (Const Char* str = nullptr);//default with parameter constructors8~myclass (void);//Default destructor9MyClass (ConstMyClass &);//Default copy constructorTenMyClass &operator=(ConstMyClass &);//default overloaded assignment operator function OneMyClass *operator& ();//default overloaded fetch operator function AMyClassConst*operator& ()Const;//default overloaded accessor operator const function -MyClass (MyClass &&);//Default Move Constructor -MyClass &operator= (MyClass &&);//Default overloaded Move assignment operator function the - Private: - Char*m_pdata; - }; + - //default with parameter constructors +Myclass::myclass (Const Char*str) A { at if(!str) - { -M_pdata =nullptr; - } - Else - { in This->m_pdata =New Char[Strlen (str) +1]; -strcpy This-m_pdata, str); to } +Std::cout <<"default with parameter constructors"<<Std::endl; - } the * //Default destructor $Myclass::~myclass (void)Panax Notoginseng { - if( This-m_pdata) the { + Delete[] This-m_pdata; A This->m_pdata =nullptr; the } +Std::cout <<"Default destructor"<<Std::endl; - } $ $ //Default copy constructor -Myclass::myclass (ConstMyClass &m) - { the if(!m.m_pdata) - {Wuyi This->m_pdata =nullptr; the } - Else Wu { - This->m_pdata =New Char[Strlen (M.m_pdata) +1]; Aboutstrcpy This-m_pdata, m.m_pdata); $ } -Std::cout <<"Default copy constructor"<<Std::endl; - } - A //default overloaded assignment operator function +MyClass & MyClass::operator=(ConstMyClass &m) the { - if( This! = &m) $ { the Delete[] This-m_pdata; the if(!m.m_pdata) the { the This->m_pdata =nullptr; - } in Else the { the This->m_pdata =New Char[Strlen (M.m_pdata) +1]; Aboutstrcpy This-m_pdata, m.m_pdata); the } the } theStd::cout <<"default overloaded assignment operator function"<<Std::endl; + return* This; - } the Bayi //default overloaded fetch operator function theMyClass * MyClass::operator&() the { -Std::cout <<"default overloaded fetch operator function"<<Std::endl; - return This; the } the the //default overloaded accessor operator const function theMyClassConst* MyClass::operator& ()Const - { theStd::cout <<"default overloaded accessor operator const function"<<Std::endl; the return This; the }94 the //Default Move Constructor theMyclass::myclass (MyClass &&m): the m_pdata (Std::move (m.m_pdata))98 { AboutStd::cout <<"default overloaded accessor operator const function"<<Std::endl; -M.m_pdata =nullptr;101 }102 103 //Default overloaded Move assignment operator function104MyClass & MyClass::operator= (MyClass &&m) the {106Assert This! = &m);107 108 This->m_pdata =nullptr;109 This->m_pdata =Std::move (m.m_pdata); theM.m_pdata =nullptr;111 theStd::cout <<"Default overloaded Move assignment operator function"<<Std::endl;113 return* This; the}
Default eight functions for C + + classes