The 4th example--c++ internal links and external links

Source: Internet
Author: User
Tags constant definition

Internal connection: If a name is local to his compilation unit and does not conflict with the same name in other compilation units at the time of the connection, then the name has an internal connection (note: Sometimes the declaration is considered to be non-connected, and here we unify as an internal connection).


The following conditions have an internal connection:


A) any statement


b) The definition of static free functions, static friend functions, static variables in namespaces (including global namespaces)


c) Enum definition


d) Definition of the inline function (including free and non-free functions)


e) Definition of class


f) Const constant definition in namespaces


g) Definition of Union


External connection: In a multi-document program, if a name can interact with other compilation units at the time of connection, then the name has an external connection.


External connections are available for the following situations:


A) class non-inline functions always have an external connection. Include class member functions and class static member functions


B) class static member variables always have an external connection.


c) namespaces (including global namespaces) non-static free functions, static friend functions, and non-static variables



The following examples illustrate:


A) Declaration, enum definition, union definition with internal connection


Any declaration, enum definition, and union definition will not generate a join symbol after compilation, that is, declarations with the same name in different compilation units and enums, union definitions do not cause errors to discover multiple symbols at the time of the connection.


typedef int INT; typedef declaration, internal Connection

Enum color{red}; Enum definition, internal connection

Union X//union Definition, internal connection

{

Long A;

Char b[10];

};

int main (void)

{

Int i = red;

return i;

}



typedef int INT; Re-declares an int type alias in a.cpp, no error occurs when connecting

Enum Color{blue}; An enum Color was redefined in a.cpp, and no error occurred while connecting

Const INT I =blue; const constant definition, internal connection

Union X//union Definition, internal connection

{

Long A;

Char b[10];

};


b) Static free functions, static friend functions, static variables, const constants in namespaces define internal connections


Namespace test

{

int foo (); function declaration, internal connection

static int i = 0; namespace static variable definition, internal connection

static int foo () {return 0;}//namespace static function definition, internal connection

}

static int i = 0; Global static variable definition, internal connection

static int foo () {return 1;}//global static function definition, internal connection

const int k = 0; Global const constant definition, internal connection

int main (void)

{

return 0;

}



Namespace test

{

int i = 0; namespace variable definition, external connection

int foo () {return 0;}//namespace function definition, external connection

}

int i = 0; global variable definition, external connection

int k = 0; global variable definition, external connection

int foo () {return 2;}//global function definition, external connection

In the global namespace, main.cpp defines the static variable i, the constant k, and the static free function foo, all of which have internal connections. If you remove the static or const modifiers of these variables or functions, they will now multiply defined symbols errors when connected, and they conflict with global variables and global functions in a.cpp.


This article is from the "very blog" blog, make sure to keep this source http://10093949.blog.51cto.com/10083949/1714778

The 4th example--c++ internal links and external links

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.