3. Use const as much as possible

Source: Internet
Author: User

</pre>1, const pointer </p><p><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><span style= "White-space:pre" >	</span> if the const appears to the left of the "*", it means that the const modifier is pointing to the data, if it appears on the right, Note that the const modifier is the pointer, the constant pointer, which can no longer point to another place. </span></p><p><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><span style= "White-space:pre" >	</span>stl iterators are also a special kind of pointer, if the form: </span></p><p ><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span></span><pre name= "code" class= "CPP" >stl::vector <int> Vec;
Const Vector<int>::iterator IT = Vec.begin ();
it++; Error, the iterator can not change;
The const modifier is the iterator itself, which means that the iterator cannot change.

If you use const_iterator, it means that the data that the iterator points to cannot be changed, and the iterator itself can change.

2, the const member function,

(1) The const is part of the function type and is also to be taken in the implementation section.

(2) The Const keyword can be used to differentiate between overloaded functions. Because each member function has an invisible this pointer in front of it, and the const modifier is the this pointer, which means that the contents of this object cannot be modified.

(3) A constant member function cannot update a member variable of a class, but it can call a member variable and cannot invoke a member function in the class that is not decorated with const, and can only invoke a constant member function

The following example example of the Const keyword overload

Class a{

Private

int w,h;

Public

int GetValue () const

{

return w*h;

}

int GetValue () {

return w+h;

}

A (int x,int y)

{

W=x,h=y;

}

A () {}

};

void Main ()

{

A const a (3,4); The const function makes it possible to manipulate a const object.

A c (2,6);

Cout<<a.getvalue () <<c.getvalue () << "Cctwltest"; Output 12 and 8

System ("pause");

}

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.