C ++ const Summary

Source: Internet
Author: User
Transferred from http://www.cnblogs.com/yedaoq/archive/2011/04/29/2032597.html1#const_cast

 

Description from msdn :

A pointer to an object or an object member can be explicitly converted to the same type with different const, volatile, or _ unaligned attributes. For pointers and references, the Conversion Result references the original object. For a pointer to a data member, the conversion result points to the object pointed to by the original pointer (before conversion. Write operations by converting the cursor pointer, reference, and (pointing to data members) pointer can lead to undefined behavior-depending on the type of the referenced object.

Const_cast converts a null pointer to a null pointer of the target type.

Summary :

Const_cast can only be applied to pointers or reference types. It seems that, in terms of semantics, const_cast only allows re-interpreting the original object, instead of generating a copy of the object. This is easy to understand: if we want to cancel the const attribute of an object, most of them are to change it. If const_cast generates a copy, the person is not empty.

Return const_cast <testconstreload> (* This ). v; // error c2440: 'const _ cast': cannot convert from 'const class xx' to 'class xx'. The same applies to built-in types.
Return const_cast <testconstreload &> (* This). V; // OK
Return const_cast <testconstreload *> (this)-> V; // OK

 

Ii. Const heavy load

For member functions, you can have the same parameter list, but the const attribute (non-parameter or returned value const attribute) of the function itself is different from the overload. For example:

ClassTestconstreload
{
Public:
Int& Val ()Const
{
Cout <"Const"<Endl;
Return const_cast<Testconstreload &> (*This). V;
}

Int& Val ()
{
Cout <"Non-const"<Endl;
ReturnV;
}

Private:
IntV;
};

 

When a member function has a const overload, the const object of the class calls the const version member function of the class. Non-const objects of the class call non-const version member functions.

If only the const member function is available, non-const objects of the class can also call the const member function. -- This idea is very descriptive. The same below.

If there are only non-const member functions, the const object of the class... No. Non-const member functions cannot be called. -- In fact, the same as the previous sentence: a const object can only call its const member function.

 

In general, when we call a member function, the compiler first checks whether the function has a const overload. If so, it determines which function to call based on the const attribute of the object. If there is no const reload, only this one will be called. In this case, the compiler also checks whether the function does not have the const attribute and the object that calls the function has the const attribute. If so, compilation fails.

 

Iii. Inheritance of const attributes

The inheritance mentioned here has nothing to do with the inheritance of the class hierarchy. It means that when we define a const object, the data member in the object will also have the const attribute (whether or not the member declaration has the const modifier ). This is easy to understand, just like a const int, where four characters cannot be changed, and all data in the const object cannot be changed. But I didn't realize this before, so it is listed here.

In the preceding sample type declaration,Int& Val ()ConstThe reason for removing the const attribute of the object at the end is that if this is not the case, the const object of the class cannot call this member function.

Int& Val ()Const{Cout <"Const"<Endl;Return const_cast<Testconstreload &> (*This). V ;}ConstTestconstreload OBJ; obj. Val (); // error: cannot convert from 'const int' to 'int'
Iv. Const compilation Optimization

PreviousArticleThis example is described as follows:

IntMain (){Const intA = 1;Int* P =Const_cast<Int*> (& A); * P = 2; cout <"° value A =" ± <A <Endl; cout <"° value * P =" ± <* P <Endl; cout <"° address a =" ± <& A <Endl; cout <"° address P =" ± <p <Endl;Return0 ;}// Output value a = 1 value * P = 2 address a = 0xbfe9efb4address P = 0xbfe9efb4 (depends on machine)

This article also provides related explanations: even if the object is stored somewhere in the memory, the compiler may actually reference this variable wherever it is referenced. If this object is small and it is a constant, the compiler knows its value at compilation. The compiler may replace the memory address with an immediate number with the same value when generating the Assembly-this can reduce the memory read operation. Original article: Use const_cast with caution

 

5. No const member functions are hosted in C ++.

If you declare a hosted C ++ class in VC. net, its member functions cannot apply the const attribute. I have not found a description about this, probably because of MISL. If you have any other information about hosting const in C ++, please let me know.

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.