Why should the inline function definition in C ++ be placed in the header file? What does "define only one copy" in the header file mean? The const object and inline functions are "one-time definition Rules"

Source: Internet
Author: User

Inline can be placed in. cpp, but only this CPP file can use it at this time.

To make a public object, you must put it in. H. If you do not want to put it in. H, you must copy one copy of each CPP file.

In fact, even if it is put in. H, it is also a copy of each CPP file, but the compiler completes this copy for you.

If the inline function is repeated, it will not cause a connection error. That is, it can be defined repeatedly. So it is an exception. Can other functions (the functions shown in the header file) be repeated?


Const in C ++ is actually a special variable. Except that you are not allowed to modify its value, there is no difference between it and the variable in other aspects (a duplicate number should be added for any two words.
The default scope of const is extern.

Const int x = 1234;

This type of header file is very common. And it is equivalent:

Extern const int x = 1234;

If you remove the const

Extern int x = 1234;

But extern int x = 1234; is not allowed to appear in the header file, because it may cause a connection error.
Extern int X; it is a variable declaration, and extern int x = 1234; it is a variable declaration plus definition. Variables can be declared repeatedly, as long as they do not conflict with each other, but can be defined only once.

However, const int x = 1234; is also the global definition of "variable" (if declared, it should be const int X;), but can appear in the header file, when multiple CPP files contain this header file, it is equivalent that each CPP file has a definition of "global variable" x. This is legal, so it is also an exception.

_---------------------------------------------------------

Inline is placed before the declaration of the function. do not add any more during implementation. inline only sends an application to the compiler, telling the compiler that my function is intended to be an inline function. The Compiler determines the result based on the actual situation. this function may be used as an inline function.

"Const object and inline functions are" one-time definition rules "exceptions? "
The implementation of the inline function is equivalent to copying a piece of code to. cpp. If it is repeatedly defined, no connection error will be generated.
The scope of the const object is limited to the current. cpp. Therefore, when you repeatedly define a const object, your own const object will appear in every. cpp object without symbol conflict!

 

---------------------------------------------------------

In C, defining a const object will allocate space, and the space has a global

Therefore, if a const is defined in the header

If the source file is included, the multiply defined symbols error is generated.

You can change the link property of static to local.

In C ++, const has a static link domain by default. Therefore, in C ++, you can

Define Const.

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.