Default eight functions for C + + classes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.