String constructor copy and destructor

Source: Internet
Author: User
The prototype of a known string is
Class string
{
Public:
String (const char * STR = NULL); // common Constructor
String (const string & other); // copy the constructor
~ String (void); // destructor
String & operate = (const string & other); // value assignment function
PRIVATE:
Char * m_date; // used to save the string
};

0

  • Reply
  • 1st Floor
  • Report |

  • Hufeng3405871
  • Intermediate FAN 2
/* Common constructor */
String (const char * STR = NULL)
{
If (STR = NULL)
{
M_date = new char [1];
* M_date = '/0 ';
}
Else
{
Int long = strlen (STR );
This-> m_date = new char [long + 1];
Strcpy (m_dat, STR );
}
}

Reply
  • 2nd floor
  • Report |

  • Hufeng3405871
  • Intermediate FAN 2

/* Copy the constructor */
String (const string & other)
{
Int long = strlen (other. m_date );
M_date = new char [long + 1];
Strcpy (m_date, other. m_date );
}

Reply
  • Third floor
  • Report |

  • Hufeng3405871
  • Intermediate FAN 2
/* Constructor */
~ String (void)
{
Delete [] m_date;
}

Reply
  • 4th floor
  • Report |

  • Hufeng3405871
  • Intermediate FAN 2
/* Value assignment function */
String operate (const string & other)
{
If (this = & other)
{
Return * this;
Delete [] m_date;
}
Else
{
Int long = strlen (other. m_date );
M_date = new char [long + 1];
Strcpy (m_date, other. m_date );
Return * this;
}
}

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.