What is the role of const in preprocessing, const, static, and sizeof-c++ (say at least 3)

Source: Internet
Author: User

1: The function is as follows:

(1) const is used to define constants: const-defined constants compilers can perform data static type security checks on them.

(2) Parameters in the form of the Const modifier: When the input parameter is a user-defined type and an abstract data type, the value pass should be changed to "const& Pass" to improve efficiency. Compare the following two sections of code:

void Fun (a a); void const &a);

The first function is inefficient. A temporary object of type A in the body of a function is used to copy parameter A, and the construction, copying, and destruction of temporary objects will consume time. The second function improves efficiency. "Pass-by-reference" does not require the creation of temporary objects, saving the temporal object's construction, replication, and time spent in the destruction process. But a reference to light can change a, so add a const.

(3) The return value of the const modifier function: If the return value of the function returned by the pointer is const, the return value cannot be directly modified, and the return value can only be assigned to a const-decorated pointer of the same type. For example:

Const Char *getchar (void) {}; Char *ch=getchar (); // Error Const Char *ch=getchar (); // correct

(4) Const modifier class member function (function definition body): Any function that does not modify the data member applies the const adornment, so that when the data member is accidentally modified or the non-const member function is called, the compiler will error. The const-decorated class has the form of a member function:

int GetCount (voidconst;

What is the role of const in preprocessing, const, static, and sizeof-c++ (say at least 3)

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.