Static inline and internal and external link objects

Source: Internet
Author: User

A discussion caused by inline

Is there a problem with the following function definition:

// Function. h

Void Hello (){

Printf ("Hello, world ");

}

 

The average person seems to be wrong, because we basically only put the function definition in CPP, then define a. h declaration, include this. h In the place of use and it will be OK. But why?

In other words, if we define Hello (in. h), and then in the two files. CPP and B. in CPP, it is included at the same time, and we are correct during compilation. When linking, we are told that Hello has been defined twice. Why?

We need to know that when you include a file, it is equivalent to copying the content of that file to the place where it is included. So if we have two files,. CPP and B. CPP, we all include the hello. h, and then compile, it is equivalent to. CPP and B. A Hello is defined in CPP, And the link fails. This is the essence of the problem

 

However, if you add an inline before hello, that is, change it

// Function. h

Inline void Hello (){

Printf ("Hello, world ");

}

Then define a. cpp and B. cpp. At the same time, incldue hello. h can be compiled and linked properly. Why? We just added an inline.

The problem is that inline indicates that a function is an internal link object. To clarify the internal link object, I have to talk about one thing.

Compilation unit:

In C or CPP, a compilation unit refers to a file that can be compiled. In C or CPP, only. C and. CPP can be compiled. Other files with any extension cannot be compiled, for example. h cannot be compiled. A file that can be compiled is a compilation unit. The so-called internal link object refers to the same name. If it appears in two compilation units but they are different, it is an internal link object. Otherwise, if the same name appears in different compilation units, the same is an external link object.

The problem above is solved after inline is added because the inline function is an internal link object and the external link object is not added.

The same problem is static. If the function is added with static, it is an internal link. If it is not added, it is an external link. The same is true for classes. If a function of a class is defined with an object of the class, it is an internal link. Otherwise, it is considered an external link.

For example:

// TT. h

# Include <iostream>

Using namespacestd;

 

Class TT {

Public:

TT ();

Public:

Void Hello ();

};

 

TT: TT (){

Cout <"TT" <Endl;

}

 

Void TT: Hello (){

Cout <"Hello, worle" <Endl;

}

If you write this statement, the class is finished and can only be included once. Otherwise, the link fails because it does not add static or Inline, that is, it is an external link object. If you change it

// TT. h

# Include <iostream>

Using namespacestd;

 

Class TT {

Public:

TT (){

Cout <"TT" <Endl;

}

 

Void Hello (){

Cout <"Hello, worle" <Endl;

}

};

This class can be used, because the function defined with the class body is an internal link object.

Of course, if it is written in the previous step, it involves class name resolution. I will not talk about class name resolution for the moment, and I will have the opportunity to write it again later.

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.