Jz-c-01

Source: Internet
Author: User

The sword refers to the offer code realization: Basic knocks, by the way write comments and questions.

First question: Add an assignment operator function to the CMyString class with the following code:

1 //============================================================================2 //Name:jz-c-01.cpp3 //author:laughing4 //Version:5 //copyright:your Copyright Notice6 //Description:hello World in C + +, Ansi-style7 //============================================================================8#include"stdafx.h"9#include <string.h>Ten using namespacestd; One /** A * Sword refers to the offer surface question 1 -  */ - classcmystring { the  Public: -CMyString (Char* PData = NULL);//constructor Function -CMyString (ConstCMyString &str);//like a copy constructor?  -~cmystring (void); +     voidPrint ();//Print -cmystring&operator=(ConstCMyString &str);//assignment operator Overloading: The return type is a reference to the type so that successive assignments can be allowed: "STR=STR1=STR2". Const: Because the state of an incoming instance is not changed within an assignment operator function + Private: A     Char*m_pdata; at }; -Cmystring::cmystring (Char*pData) { -     if(PData = = NULL) {//if it is empty -M_pdata =New Char[1]; -M_pdata =' /';//string Default last fill ' \ ' -}Else { in         intLength = strlen (PData);//string Length -M_pdata =New Char[Length +1];//why +1? tostrcpy (M_pdata, pData);//Copy +     } - } theCmystring::cmystring (ConstCMyString &str) { *     intLength =strlen (str.m_pdata); $M_pdata =New Char[Length +1];Panax Notoginseng strcpy (M_pdata, str.m_pdata); - } thecmystring::~cmystring () { +     Delete[] m_pdata;//Freeing Memory A } the voidcmystring::p rint () { +cout << m_pdata <<Endl; - } $cmystring& cmystring::operator=(Constcmystring& str) {//automatic generation, with inline in front, why?  $     if( This= = &str) {//first determine if the parameter passed in and the current instance is not the same instance ★ -         return* This; -     } the     /*If there is no above if judgment, if the same instance, once the memory of the release itself, the memory of the passed parameter is also released*/ -     Delete[] m_pdata;//Free Memory FirstWuyiM_pdata =NULL; theM_pdata =New Char[Strlen (Str.m_pdata) +1];//Reassign Space - strcpy (M_pdata, str.m_pdata); Wu     return* This;//returns a reference to the instance itself - } About intMain () { $cout <<"Hello World!!!"<< Endl;//prints Hello World!!! -     Char* str ="Hello World";//What's the problem, and not the const difference - cmystring Test (str); - test.print (); A cmystring test2, test3; +     //test = test;//assign to yourself, here to determine whether the incoming parameter and the current instance will not have the same problem theTest3 = Test2 = Test;//Assigning a value to an object test2,test3 using an assignment operator - test3.print (); $     return 0; the}

Jz-c-01

Related Article

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.