A little experience with ISO C + + operator= overload __c++

Source: Internet
Author: User

        recently in writing a class, very depressing is the need to overload = operator, on the internet for a long time did not have a good tutorial, only to explore their own, the original code is written like this://define Class   class cstring {   public:       cstring ();        ~cstring ();       CString& operator=  (const cstring&);       int lenght ();       char* _char ();    private:       char* value;       unsigned int strsize; }; cstring& cstring::operator=  (CONST CSTRING& STR) {   if (this ==  &STR)       return *this;    int len = strlen (Str._char ());            //The problem is right here      delete this->value;    this->value = new char[len + 1];    memset (this->value,0,len + 1);    memcpy (This->value,str._char (), Len)      //problem still appears here       this->strsize = len;    return *this; }

is to prompt 19 rows and 24 line errors at compile time: type qualification is discarded when ' const CString ' is the ' this ' argument of ' char* Cstring::_char () '

Search the network and not be able to find a detailed tutorial on the operator= overload. In their own blind touch, found that the operator= overload when passed to (const cstring& STR) can actually access his private members, why, I am not clear to the present, Because this member variable is not global and the operator= overload is not a friend, it cannot be declared as a friend. (If a master knows what the reason is, I hope to enlighten younger brother)

To get to the point, the problem is that I've called the STR member function _char (), causing the pass of the const CString this is lost. At first I don't know if I can access its private members, I accidentally tried to access its private members at the end of the way, At that time I thought that since he would have caused this pointer to be lost, he must have passed a this pointer, the this pointer could access the private member, and then directly access STR's private member variable value, and finally succeeded. What a helpless death ...

Although you can implement the CString object assignment to the CString object, it is not clear whether my direct access to its private members can lead to sequelae. For example, it is possible to access the private members of an object directly, and not to create unsafe code problems. or Linux ISO C + + is like this ..... Continue to study .....

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.