Skip to content

Commit dbca977

Browse files
TaojunshenTylerMSFTTylerMSFT
authored
fix code sample (#3676) (#3677)
* fix code sample * acrolinx Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: Tyler Whitney <[email protected]> Co-authored-by: TylerMSFT <[email protected]>
1 parent 20febbf commit dbca977

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

docs/cpp/declarations-and-definitions-cpp.md

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
---
22
description: "Learn more about: Declarations and definitions (C++)"
33
title: "Declarations and definitions (C++)"
4-
ms.date: "12/12/2019"
5-
ms.assetid: 678f1424-e12f-45e0-a957-8169e5fef6cb
4+
ms.date: 07/23/2021
65
---
76
# Declarations and definitions (C++)
87

9-
A C++ program consists of various entities such as variables, functions, types, and namespaces. Each of these entities must be *declared* before they can be used. A declaration specifies a unique name for the entity, along with information about its type and other characteristics. In C++ the point at which a name is declared is the point at which it becomes visible to the compiler. You cannot refer to a function or class that is declared at some later point in the compilation unit. Variables should be declared as close as possible before the point at which they are used.
8+
A C++ program consists of various entities such as variables, functions, types, and namespaces. Each of these entities must be *declared* before they can be used. A declaration specifies a unique name for the entity, along with information about its type and other characteristics. In C++ the point at which a name is declared is the point at which it becomes visible to the compiler. You can't refer to a function or class that is declared at some later point in the compilation unit. Variables should be declared as close as possible before the point at which they're used.
109

1110
The following example shows some declarations:
1211

1312
```cpp
1413
#include <string>
1514

16-
void f(); // forward declaration
15+
int f(int i); // forward declaration
1716

1817
int main()
1918
{
@@ -37,19 +36,19 @@ namespace N {
3736
3837
On line 5, the `main` function is declared. On line 7, a **`const`** variable named `pi` is declared and *initialized*. On line 8, an integer `i` is declared and initialized with the value produced by the function `f`. The name `f` is visible to the compiler because of the *forward declaration* on line 3.
3938
40-
In line 9, a variable named `obj` of type `C` is declared. However, this declaration raises an error because `C` is not declared until later in the program, and is not forward-declared. To fix the error, you can either move the entire *definition* of `C` before `main` or else add a forward-declaration for it. This behavior is different from other languages such as C#, in which functions and classes can be used before their point of declaration in a source file.
39+
In line 9, a variable named `obj` of type `C` is declared. However, this declaration raises an error because `C` isn't declared until later in the program, and isn't forward-declared. To fix the error, you can either move the entire *definition* of `C` before `main` or else add a forward-declaration for it. This behavior is different from other languages such as C#, in which functions and classes can be used before their point of declaration in a source file.
4140
42-
In line 10, a variable named `str` of type `std::string` is declared. The name `std::string` is visible because it is introduced in the `string` [header file](header-files-cpp.md) which is merged into the source file in line 1. `std` is the namespace in which the `string` class is declared.
41+
In line 10, a variable named `str` of type `std::string` is declared. The name `std::string` is visible because it's introduced in the `string` [header file](header-files-cpp.md), which is merged into the source file in line 1. `std` is the namespace in which the `string` class is declared.
4342
44-
In line 11, an error is raised because the name `j` has not been declared. A declaration must provide a type, unlike other languages such as javaScript. In line 12, the **`auto`** keyword is used, which tells the compiler to infer the type of `k` based on the value that it is initialized with. The compiler in this case chooses **`int`** for the type.
43+
In line 11, an error is raised because the name `j` hasn't been declared. A declaration must provide a type, unlike other languages such as JavaScript. In line 12, the **`auto`** keyword is used, which tells the compiler to infer the type of `k` based on the value that it's initialized with. The compiler in this case chooses **`int`** for the type.
4544
4645
## Declaration scope
4746
48-
The name that is introduced by a declaration is valid within the *scope* where the declaration occurs. In the previous example, the variables that are declared inside the `main` function are *local variables*. You could declare another variable named `i` outside of main, at *global scope*, and it would be a completely separate entity. However, such duplication of names can lead to programmer confusion and errors, and should be avoided. In line 21, the class `C` is declared in the scope of the namespace `N`. The use of namespaces helps to avoid *name collisions*. Most C++ Standard Library names are declared within the `std` namespace. For more information about how scope rules interact with declarations, see [Scope](../cpp/scope-visual-cpp.md).
47+
The name that is introduced by a declaration is valid within the *scope* where the declaration occurs. In the previous example, the variables that are declared inside the `main` function are *local variables*. You could declare another variable named `i` outside of main, at *global scope*, and it would be a separate entity. However, such duplication of names can lead to programmer confusion and errors, and should be avoided. In line 21, the class `C` is declared in the scope of the namespace `N`. The use of namespaces helps to avoid *name collisions*. Most C++ Standard Library names are declared within the `std` namespace. For more information about how scope rules interact with declarations, see [Scope](../cpp/scope-visual-cpp.md).
4948
5049
## Definitions
5150
52-
Some entities, including functions, classes, enums, and constant variables, must be defined in addition to being declared. A *definition* provides the compiler with all the information it needs to generate machine code when the entity is used later in the program. In the previous example, line 3 contains a declaration for the function `f` but the *definition* for the function is provided in lines 15 through 18. On line 21, the class `C` is both declared and defined (although as defined the class doesn't do anything). A constant variable must be defined, in other words assigned a value, in the same statement in which it is declared. A declaration of a built-in type such as **`int`** is automatically a definition because the compiler knows how much space to allocate for it.
51+
Some entities, including functions, classes, enums, and constant variables, must be defined as well as declared. A *definition* provides the compiler with all the information it needs to generate machine code when the entity is used later in the program. In the previous example, line 3 contains a declaration for the function `f` but the *definition* for the function is provided in lines 15 through 18. On line 21, the class `C` is both declared and defined (although as defined the class doesn't do anything). A constant variable must be defined, in other words assigned a value, in the same statement in which it's declared. A declaration of a built-in type such as **`int`** is automatically a definition because the compiler knows how much space to allocate for it.
5352
5453
The following example shows declarations that are also definitions:
5554
@@ -70,7 +69,7 @@ public:
7069
};
7170
```
7271

73-
Here are some declarations that are not definitions:
72+
Here are some declarations that aren't definitions:
7473

7574
```cpp
7675
extern int i;
@@ -79,15 +78,15 @@ char *strchr( const char *Str, const char Target );
7978
8079
## Typedefs and using statements
8180
82-
In older versions of C++, the [`typedef`](aliases-and-typedefs-cpp.md) keyword is used to declare a new name that is an *alias* for another name. For example the type `std::string` is another name for `std::basic_string<char>`. It should be obvious why programmers use the typedef name and not the actual name. In modern C++, the [`using`](aliases-and-typedefs-cpp.md) keyword is preferred over **`typedef`**, but the idea is the same: a new name is declared for an entity which is already declared and defined.
81+
In older versions of C++, the [`typedef`](aliases-and-typedefs-cpp.md) keyword is used to declare a new name that is an *alias* for another name. For example, the type `std::string` is another name for `std::basic_string<char>`. It should be obvious why programmers use the typedef name and not the actual name. In modern C++, the [`using`](aliases-and-typedefs-cpp.md) keyword is preferred over **`typedef`**, but the idea is the same: a new name is declared for an entity, which is already declared and defined.
8382
8483
## Static class members
8584
86-
Because static class data members are discrete variables shared by all objects of the class, they must be defined and initialized outside the class definition. (For more information, see [Classes](../cpp/classes-and-structs-cpp.md).)
85+
Because static class data members are discrete variables that are shared by all objects of the class, they must be defined and initialized outside the class definition. (For more information, see [Classes](../cpp/classes-and-structs-cpp.md).)
8786
8887
## extern declarations
8988
90-
A C++ program might contain more than one [compilation unit](header-files-cpp.md). To declare an entity that is defined in a separate compilation unit, use the [`extern`](extern-cpp.md) keyword. The information in the declaration is sufficient for the compiler, but if the definition of the entity cannot be found in the linking step, then the linker will raise an error.
89+
A C++ program might contain more than one [compilation unit](header-files-cpp.md). To declare an entity that is defined in a separate compilation unit, use the [`extern`](extern-cpp.md) keyword. The information in the declaration is sufficient for the compiler, but if the definition of the entity can't be found in the linking step, then the linker will raise an error.
9190
9291
## In this section
9392

0 commit comments

Comments
 (0)