extern declaration variables in C + + _c language

Source: Internet
Author: User
Tags function definition

The extern declaration variable is no different from the following two kinds:

1. Declaring Global variables
2. Declaring functions

Today we only talk about extern, what const, static and so on and so on and related or unrelated to all ignore, the following on the above two kinds of situation to explain each

Declarations and definitions
Now that we're talking about extern declaration variables, we have to figure out the difference between the declaration and the definition.

Here we will general data variables and functions collectively called variables. From the memory allocation perspective, the difference between declarations and definitions is that declaring a variable does not allocate memory, and defining a variable allocates memory. A variable can be declared multiple times, but it can only be defined once.

Based on the above premise, we can make the declaration and definition analogy to the relationship between the pointer and memory. We know that the pointer is actually a symbol pointing to memory, the definition of a variable is like an area of memory, and a declaration is like a pointer to a single memory region, and a pointer can only point to one area of memory, which makes it easy to understand why a variable can only be defined once, if it is defined multiple times. It would be a problem to allocate more than one memory so that you can find out which memory area you want to go through the variable declaration.

For data, declarations and definitions often exist at the same time, such as the following line of statements

Copy Code code as follows:

int data;

In this way, both the data and the data are declared, and how to make the declaration and not define it, with extern is OK.
Copy Code code as follows:

extern int data;

For functions, declarations and definitions are easy to distinguish, and in general we place the declaration in the header file and the definition in the source file.
Copy Code code as follows:

void Hello ();

This is a declaration of a function, and
Copy Code code as follows:

void Hello ()
{
printf ("Hello world!\n");
}

This is the definition of a function. Of course, the Declaration and definition of a function can also occur simultaneously, if we do not have a header file and only a source file, and there is no void hello () in the source file, then the declaration and definition of the function occurs at the same time, if we want to invoke the function hello () in the original file. The code you call must be after the function definition.

In fact, the above point is only one sentence: You must declare that the declaration can be multiple times, and the definition can only be one time before using a variable. Remember this sentence, the back is very easy to understand.

extern Declaration global variable

Let's take a look at the following examples of existing three files: Test.h, Test.cpp, Main.cpp, where main.cpp and test.cpp need to share a variable g_name, three files are as follows

Copy Code code as follows:

* Test.h * *
#ifndef _test_h_
#define _test_h_

#include <string>

Std::string G_name;
void Hello ();

#endif

* Test.cpp * *
#include <stdio.h>
#include "test.h"

void Hello ()
{
printf ("Hello%s!\n", g_name.c_str ());
}

* main.cpp * *
#include "test.h"

Std::string G_name;

int main ()
{
G_name = "Handy";
Hello ();
return 0;
}

The relationship between the three, Test.cpp contains the Test.h,main.cpp also contains the test.h, where the inclusion is actually include. We execute the compile command

Copy Code code as follows:

g++ main.cpp Test.cpp

Compile an error redefinition of ' g_name ', which says that G_name has been redefined

Let's take a look at where G_name appears, one in Test.h, one in main.cpp, and two statements in std::string G_name, as we have said before, in such a way as to declare and define variables, how g_name is redefined, First we need to understand the meaning of include, we can understand that include a header file in the line to expand all the code in the header file, Since main.cpp contains test.h, we will be test.h in that row, we'll find that there are two std::string g_name in main.cpp, so main.cpp is defined two times in G_name.

Since we can interpret the include header file as the expansion code, we actually do not need to specify the header file when compiling, just need the source file is enough. It is important to note that redefining does not mean defining multiple times in the same original file, but in the entire code space, such as the example above, which refers to Test.cpp and main.cpp, in fact, the above example G_name is redefined three times, One of the test.cpp, main.cpp two times.

How to solve the above redefined problem, very simple answer, will test.h in the std::string g_name, to the extern std::string g_name; Because the extern statement declares only the variable and does not define the variable, So test.cpp and main.cpp to expand the header file, but only the g_name declared two times, and the real definition is still in main.cpp

extern declaration function

Or the example above, we can call the Hello function in the main.cpp without a header file, since today's theme is extern, do not need to remind also know, use extern on it, the code is as follows

Copy Code code as follows:

* Test.cpp * *
#include <string>
#include <stdio.h>

Statement G_name
extern std::string G_name;

Declaring and defining void hello ()
void Hello ()
{
printf ("Hello%s!\n", g_name.c_str ());
}

* main.cpp * *
#include <string>

Declaration and definition G_name
Std::string G_name;

Declaration void Hello ()
extern void Hello ();

int main ()
{
G_name = "Handy"
Hello ();
return 0;
}

Note that there are two scenarios for extern declaration variables and functions, which I have commented on separately after the statement. Compile commands as follows

Copy Code code as follows:

g++ main.cpp Test.cpp

Here we do not use the header file, but can still share the variables and functions between different files, all of this is extern credit!

Summarize

To understand extern, the following concepts are mainly understood:

1, the difference between the declaration and the definition. In the Global code space, a variable can have multiple declarations, but only one definition
2, include header file is equivalent to expand the header file code

Understanding the above two points, and then analyze the use of extern, it will be a lot clearer

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.