C++ | How to distinguish between declaration and definition in C++?

In the C++ coding process, we often talk about "definition" and "declaration", both of which are the basic concepts in the programming process. When we need to use a variable, type (class, structure, enumeration, union) or function, we need to define and declare it in advance.

The process of definition and declaration is just like when we borrow books from a library. We need to complete the printing of books, that is, to create books. This is a process of definition. With books, we need to go to the library to complete the registration procedures for borrowing. This is the process of declaration. After completing the statement, we have the authority to use books, and we can swim in the ocean of knowledge as much as we want. If the book is printed by your own entrusted printing house, then you do not need to borrow from others, that is, you can use the book directly without a statement.

A book only needs to be printed once, but it can be borrowed multiple times by multiple people, which means that the definition only needs one time, but the declaration can have multiple times. The books here refer to the objects of "definition" and "declaration", namely variables, types and functions. In C/C++, a variable, type or function must be defined and declared before use.

 

Definition and declaration are concepts that are easy to confuse, but through the above analogy, it can be seen: 

(1) The essential difference between "definition" and "declaration" is that the declaration can appear multiple times, while the definition can only appear once; 

(2) Put the declared things in the header file, and put the defined things in the source file (.c or .cpp file); 

(3) The definition of the type should be placed in the header file, because the type does not have external connectivity, and different source files with the same type definition will not report a compilation error, but the header file cannot be included repeatedly.

1. Definition and declaration of variables

Define variables, specify their type, name, allocate memory space and initialize their initial value, such as

int a=1;

int a(1);

If the initialization is not displayed, it will be initialized according to the compiler default.

Declare variables, specify the type and name of the variable, such as:

extern int a;

One thing to note is that local variables and global static variables cannot be pre-declared through extern, that is, they cannot be referenced by declaration before definition, because the scope of local variables is the current code block, and the scope of global static variables is The current source files are not in the global scope, so they cannot be forward-declared through extern. Global variables are allowed to be referenced through forward declarations before they are defined. See the following code snippet.

#include <stdio.h>

extern int a;

extern static int b; //Report an error

int main ()

{

extern int c; //Report an error

printf("a=%d,b=%d,c=%d",a,b,c);

int c=2;

}

int a=0;

static int b=1;

 

 

2. Type definition and declaration

Define the type, specify the name and content of the type.

struct test{int a;}

Or give an alias to an existing type.

typedef int int32;

Note that the scope of the type is the source file, that is, the type does not have the nature of external connection, so you can define the type with the same name in different source files. For example, defining a class with the same name will not report a redefinition error, which also explains the type The definition should be placed in the header file, but if the type with the same name is defined in the same source file, the compiler will report a redefinition error.

Declare the type and only give the name of the type.

class A;

After the type is declared, it can be used to declare other identifiers, but it cannot be used to define objects, nor can its members be used.

class A; //Declare the class first

void show(A& a); //Declare function

class A //define later

{

public:

int a;

char b;

};

There is no error in the above code. But the following code will report an error:

class A; //declare first

int main(int argc,char* argv[])

{

A classA;

classA.a = 5;

return 0;

}

class A //define later

{

public:

int a;

char b;

};

The program cannot be compiled because the effective range of all members of class A starts from the place where the class is defined, so it is an error to initialize its member variables with an unknown constructor, and an error of using undefined class "A" will be reported. .

Just give the definition before using the type, that is, give the name and content of the type, or use typedef to give a type a unique name. Although it is a definition type, the definition of the type is still placed in the header file. Even if it is included in a different source file, because the type does not have external connection characteristics, it will not report a redefinition error. This is different from variables and functions. Put the definition of variables and functions in the header file! Remember! The difference between the external connection and the internal connection mentioned here is whether the linker compares the definition in the current target file with other target files when connecting, and reports whether there is a redefinition error. Internal connections will not be compared, so no error will be reported for types with the same name defined in different source files.

 

3. Function definition and declaration

Define the function, specify the function return type, function name, function parameters and function body.

int test(char a,int b)

{

return a+b

}

Declare a function, specify the function return type, function name and function parameters.

int test(char a,int b);

//Or no need to give the parameter name, only the type

int test(char,int);

As can be seen from the above, the difference between function definition and function declaration is mainly in two points: 

(1) The function definition needs to give the function body, that is, the concrete realization of the function, the function declaration does not need; 

(2) The function definition must give the parameter name, and the declaration can only give the parameter type.

The above is a detailed introduction to distinguish between declarations and definitions in c++. Welcome everyone to make valuable comments on distinguishing the content of declarations and definitions in c++.

 

If you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster!

[ C language C++ learning penguin circle ], share (source code, project actual combat video, project notes, basic introductory tutorial)

Welcome partners who change careers and learn programming, use more information to learn and grow faster than thinking about it yourself!

Programming learning books:

 

Programming learning video:

 

Guess you like

Origin blog.csdn.net/Hsuesh/article/details/112863344