Clause 28: avoid returning handles pointing to internal components of the object

Source: Internet
Author: User

Let's first look at an example:

Classpoint {

Public:

Point (intx, inty );

Voidsetx (intnewval );

Voidsety (intnewval );

};

 

Structrectdata {

Pointulhc;

Pointlhc;

};

Classrectangle {

Public:

Point & upperleft () const {returnpdata-> ulhc ;}

Point & lowerright () const {returnpdata-> lrhc ;}

...

PRIVATE:

STD: tr1: shared_ptr <rectdata> pdata;

};

 

 

The upperleft () and lowerright () methods in the rectangle class return the upper left vertex and lower right vertex. We do not want customers to modify it, so we use the const keyword to limit it.

However, we find that the two functions return references to the internal data of pdata. Therefore, it is not encapsulated. In other words, we can do this:

Pointcoord1 (1, 1 );

Pointcooracle D2 (100,100 );

Constrectanglerec (coord1, coord2 );

Rec. upperleft (). setx (50 );

We can still modify it. Although that function is a const. In this example, we can learn the following lessons:

L The encapsulation of member variables will be broken by references.

L if the const member function has a reference, and the data referred to by the latter is associated with the object itself, and it is stored outside the object, the caller of the function can modify the data.

L Similarly, pointer is returned, and iterator has the same effect as reference.

So we can modify it as follows:

Classrectangle {

Public:

Constpoint & upperleft () const {returnpdata-> ulhc ;}

Constpoint & lowerright () const {returnpdata-> lrhc ;}

...

PRIVATE:

STD: tr1: shared_ptr <rectdata> pdata;

};

In this way, the above problem is solved: that is, these functions transfer the read permission, but the write permission is still disabled.

However, this is not very good, because upperleft and lowerright still return handles "representing the object", which may cause problems in other scenarios. More specifically, it will lead to "dangling handles (an empty number plate): This kind of handles indicates that the object does not exist". Consider the following example:

Classguiobject {};

Constrectangleboundingbox (constguiobject & OBJ );

Now, the customer may use this method.

Guiobject * PGO;

Constpoint * pupperleft = & (boundingbox (* PGO). upperleft ());

 

You will find that (boundingbox (* PGO). upperleft () is a point object. However, after this sentence is executed, the temporary object will be destructed. In this case, pupperleft points to an empty object. Then there is a suspension.

 

Therefore, this is why the function "returning a handle represents the internal component of the object" is always dangerous.

But this does not mean that the function cannot return the internal handles. Sometimes you must do this. For example:

 

Classfoo {

Public:

Int & operator [] (constint );

Constint & operator [] (constint) const;

PRIVATE:

Vector <int> data;

}

Int & FOO: operator [] (constinti ){

Returndata [I];

}

Constint & FOO: operator [] (constinti) const {

Returndata [I]

}

Remember:

    • Avoid returning handles pointing to the object. Compliance with this clause can increase encapsulation, help const member functions look more like a const, and minimize the possibility of "virtual number plate.

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.