Differences between definitions and declarations

Source: Internet
Author: User

 

The explanation of C ++ primer is as follows:

Definition: used to allocate storage space for a variable. You can also specify an initial value for the variable. InProgramThe variable has only one definition;

Variable Declaration: used to indicate the type and name of the variable to the program. Definition is also Declaration: when defining a variable, we declare its type and name. You can declare a variable name without defining it by using the extern keyword.

The above explanation is quite clear. The definition of allocated space is called declaration. Well, you don't know when to allocate it. Let's make another statement.

"Definition is Declaration", which indicates that the Declaration includes definition. So declarations such as int A; extern int A; must be used. Then let's look at the definition;

If no variable A exists before the program, you must let the program know that you want to use the variable. At this time, you write int A; in the past, there was no such variable as A. Now, in order to remember it, the program will allocate space for it, so this is a definition.

If a already exists in other files in the program, this proves that the program has allocated memory for a. In this case, it is much easier to use. You only need to tell the program that this a has been defined elsewhere, so you write extern int;

For int A;, it is both a definition and a declaration; for extern int A;, It is a declaration, not a definition. Generally, the bucket creation declaration is defined to facilitate the description, rather than the bucket creation declaration.

 

[Additional Reading] I will discuss the C ++ statements and definitions.

In C ++ primer version 4, exercise 2.18 on page 53 asked whether extern STD: string name is a declaration or definition? The answer is declaration. However, in my opinion, the string class has a default constructor. If this statement is outside the function, the bucket will be automatically obtained and the name will be defined as an empty string. What is your opinion?

Question added:

Thanks for answering my questions about extern, but I still haven't figured it out. For example, in the example above, extern STD: tring name ("zhang3li4"); I think extern STD: string name; is the same as that sentence. because the string in the standard library has a default constructor, if there is nothing behind it, it is initially a null string.

------

I did an experiment just now:
// A. cpp
# Include <string>

STD: string I;

// B. cpp
# Include <iostream>
# Include <string>

Extern STD: string I;

Int main ()
{
STD: cout <I <STD: Endl;
}

The running result is:

(The above is an empty row: Because Endl is used at the end)

If the file a. cpp does not exist, an error occurs.
This is an experiment (whether there is a. cpp file)

We can conclude that:
A. STD: string I; in CPP is defined (originally)
B. extern STD: string I; in CPP is declared (your problem)

Don't worry. This is only the result of the experiment. We still need a theoretical basis, so let's try to explain it below:

The following is another example (the second experiment)
You should know that the built-in type (INT, etc.) is initialized at the same time as the global variable (a built-in type has its default value; if you think of a custom type as a built-in type, it means that a custom type has its default value (completed by the default constructor )). For example
// C. cpp
Int I;

// D. cpp
# Include <iostream>

Extern int I;

Int main ()
{
STD: cout <I <STD: Endl;
}

In this case, int I; is defined (and I is initialized, and the initial value is 0)
Therefore, the output result is
0
// The running result of this experiment conforms to the theoretical basis.
(The Compiler I use is Visual Studio 2008)

From the two experiments: (the two "default" are equivalent)
Int I;
STD: string I;
Can be used as a Definition Statement for initializing I.
So
Extern int I;
Extern STD: string I;
Also has a defined behavior (this is obviously contrary to the use of extern)

In the above two experiments: Is int different from string? AllCodeThey are all the same. They only replace STD: string with Int! If you can explain the int situation, I think the STD: String problem will be solved!

So I personally concluded that:
Whether it is a built-in type or a custom type, there is an extern keyword before. If there is no explicit initialization parameter (that is, the default constructor of the custom type is blocked by extern, initialization is ineffective.

my description may be messy. I hope you can take a good look at the above example and understand it!
note: the particularity of the default constructor can be seen here

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.