The definition of the inline function in C + + can be placed in the header file, and the inline function repeats without causing a connection error.

Source: Internet
Author: User
Tags function definition

Attached: http://blog.csdn.net/yingxunren/archive/2009/10/13/4663014.aspx

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

If you want to make it public, you must put it in. h, and if you do not want to put it in. h, you must copy each CPP file.

In fact, even if you put it in. h, it's a copy of each CPP file, except that the compiler completes the copy for you.

Inline function recurrence does not cause connection errors, that is, you can repeat the definition, so the exception is, other functions (refers to the function can be seen in the header file) can be repeated definition.

The const in C + + is actually a special variable, except that you do not allow you to modify its value, otherwise there is no difference with the variable (any two words should be highlighted).(The following paragraph I think some of the wrong statement)

The default scope for const is extern.(I think it's not right, the default scope of const should not be extern, but this file, for internal connection properties)


const int x = 1234;

This kind of Dongdong header file is very common. And it's equivalent to:

extern const int x = 1234;

If you remove the const, it becomes

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;   is a variable declaration, extern int x = 1234; is a variable declaration plus a definition. Variables can be declared repeatedly, as long as they are not contradictory, but defined only once.

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

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

The inline is placed in front of the function's declaration. Don't add it when it's implemented. Inline just sent a request to the compiler telling the compiler that my function wanted to be an inline function, and the compiler looked at the specifics to make a decision. This function can be used as an inline function or not.

"The const object and the inline function are the exceptions to the" once defined rule "and what does it mean. "
The inline function, when implemented, is equivalent to copying a piece of code into. CPP, and if it is repeatedly defined, it will not cause a connection error.
The scope of the const object is limited to the current. cpp. So when you repeat the definition, every. cpp will appear with its own const object, no symbolic conflict.

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

In C, defining a const object allocates space, and this space has a global

Domain, so if a const is defined in the header, then if the header is multiple C

If the source file contains words, it will produce multiply defined symbols errors.

It can be done by adding static to the part of the connection attribute.

In C + +, the const default has a static link field, so in C + + you can

Define Const.

This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/rufeng18/archive/2007/11/23/1898873.aspx

Inline functions must be used in conjunction with the function definition, and the caller must be able to see the definition of the function, which should be placed in the definition, the declaration does not need.

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.