The this pointer inside a common function in C + + is also a const type

Source: Internet
Author: User

There is a strange phenomenon in the code that calls the this pointer in the same function, and the result is one that cannot be compiled

1 //Read connection Information2 voidthirdwizardpage::readconnection ()3 {4QFile file ("oracle.passwd");5     if(!File.Open (qiodevice::readonly))6     {7Qmessagebox::information ( This, TR ("failed to open file"),8Tr"cause of error:%1"). Arg (file.errorstring () ));9         return;Ten     } OneQdatastreaminch(&file); A Connection con; -     inti =0; -      while(!inch. AtEnd ()) the     { -         inch>>con; -Ui->combobox->InsertItem (i, con.strdatabase); - M_mapconnections.insert (I, con); +     } - file.close (); + } A  at //Write connection Information - voidThirdwizardpage::writeconnection ()Const - { -QFile file ("oracle.passwd"); -     if(!File.Open (qiodevice::writeonly)) -     { inQmessagebox::information (const_cast<thirdwizardpage*>( This), TR ("failed to open file"), -Tr"reason for failure:%1"). Arg (file.errorstring () )); to         return; +     } -Qdatastream out(&file); the  *      for(inti =0; i < m_mapconnections.size (); + +i) $     {Panax Notoginseng          out<<M_mapconnections[i]; -     } the file.close (); +}

The compile error prompt is:

The function implemented by the above two functions is a read file, a write file, in two functions are used in the Qmessagebox::information function, but the second function must be forced to convert the this pointer to non-const to compile, think carefully about the problem found is because the function definition written to the file is used to decorate the function as a constant function, that is, before the curly braces of the function body.

Previously, the use of C++primer was only known to be defined as a constant function to prevent the function from modifying the member variables of the class object. Just know that if you modify the class member variable in the constant function, then compile time is not pass, as for why, how to achieve such control, but do not know, only remember how to use.

Here's my own analysis, as we all know, all member functions of C + + have an invisible pointer to the function, the pointer to the object (except static function), then you can speculate that the member function of the class access to the member or through a pointer to access, even if sometimes we do not display the use of the This pointer , the system may also be filled in for us by default, because we want to manipulate memory, in the final analysis of where it is stored, so that is the language without pointers, I think, it is the platform for it to block these things, and not these things do not exist (these things refer to pointers).

Whether this can be concluded: in the constant function, the system default incoming function of the this pointer is a const type.

Then, this can be explained, if you modify the member variable in the constant function (even if you do not display the use of this pointer, the system will add, because to access memory, you must use a pointer), and now the this pointer is a const type, then will be an error, Therefore, it is possible to implement the ability to restrict the modification of member variables in the constant function.

Of course, the above is all by virtue of their own view of the phenomenon and the programming of some perception, understanding of the speculation, do not know right. But my idea is, since the master can think out, and the master is also a person, but more experience, so if they think more, may also become a master, hey, wish.

Recently began to look deep into the C + + object model, that is, how the C + + object model is implemented, I hope in this book can be the conclusion of their own to verify.

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.