Describes how to optimize C ++ compiler variables.

Source: Internet
Author: User

In most cases, caching variables in registers is a very valuable optimization method. If not, it is a pity. The C ++ compiler provides the opportunity to explicitly disable such cache optimization. If you declare that the variable uses the volatile modifier, the compiler will not cache the variable in the Register-the actual location of the variable in the memory will be accessed each time.

It prevents the C ++ compiler from optimizing the modified variables. It is mainly used in multi-threaded programming. Volatile can be used to modify native types or to customize types. Although volatile has different semantics with const, its usage is similar.

 
 
  1. class Gadget  
  2. {  
  3. public:  
  4.  void Foo() volatile{};  
  5.  void Bar() const{};   
  6.  void Doo(){};  
  7. //private:  
  8.     char name_;  
  9.     int state_;  
  10. }; 

If an object is defined as const: const Gadget cGadget, the value of the member variable of the cGadget object cannot be changed. Therefore, this object must only call const-type member functions in the interface. That is, the interface that can be called cannot modify the value of its member variable. The member function must be of the const type. That is:

 
 
  1. class Gadget  
  2. {  
  3. public:  
  4.  void Foo() volatile{};  
  5.  void Bar() const{};   
  6.  void Doo(){};  
  7. //private:  
  8.     char name_;  
  9.     int state_;  
  10. }; 

Because the const type variable requires that its calling interface be const type, and the value assignment function needs to be called between objects, the C ++ Compiler's value assignment function is not const type, so Overloading is inevitable. However, the ridiculous problem is that a const object requires that the value of the member variable cannot be changed, but the value assignment is to change the value.

So the value assignment function (constructor) cannot assign values to variables. If you do not assign values to variables in the value assignment function, you can compile the variable. This is meaningless, but it is only volatile, because volatile is the same in const operations, but volatile can change the value of member variables, so it is not a problem here.

This operator converts expression to the type-id type, but does not check the runtime type to ensure the conversion security. It has the following usage:
① It is used to convert pointers or references between classes and subclasses in the class hierarchy. It is safe to convert the pointer or reference of a subclass into a base class. When a base class pointer or reference is converted to a subclass in a downstream conversion, because there is no dynamic type check, therefore, it is insecure.
② It is used for conversion between basic data types. For example, convert int to char and convert int to enum. The security of such conversions must also be ensured by developers.
③ Convert a null pointer to a null pointer of the target type.

  • In-depth analysis of how C ++ compiler implements Exception Handling
  • C ++ compilation mode description
  • Better Design of object-oriented C ++ Language
  • Describes the features of C ++ compiling language to the greatest extent.
  • Analysis of Native C ++ in Windows Embedded CE

④ Convert any type of expression to void type.

Note: The C ++ compiler cannot convert the const, volitale, or _ unaligned attributes of expression.

  1. How to Write C ++ project development and project plan correctly
  2. Summary Notes on learning and exploring C ++ library functions
  3. In-depth demonstration of high security of C ++
  4. Describes in detail how to accurately Write C ++ languages.
  5. In-depth demonstration of high security of C ++

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.