1.Briefly explain the role of namespaces in C++.
A: Avoid the problem of duplicating the definition of global variables.
2. Define two namespaces A and B. Define the variable value in A and B. Print out the value of the two namespaces in the main function.
#include "iostream"
using namespace std;
namespace A
{
int value = 100;
}
namespace B
{
int value = 10;
}
int main()
{
cout << "namespaceAcentervaluebe valued at:" << A::value << endl;
cout << "namespaceBcentervaluebe valued at:" << B::value << endl;
}
3. const int a; Does it need to be initialized in C++ compiler and why?
Initialization is required because const-modified variables are read-only attributes that do not allow the variable a to be assigned a value after it is defined, i.e., if a variable is not assigned a value at the same time as it is defined, the value of a defaults to 0 and is not allowed to be changed subsequently.
4、How to use c library functions in c++?
Method I:
Write conditional compilation in .c file
#ifdef _cplusplus
Extern "C"{
..................
} (write the c library function and its code in a compound statement)
#endif
Method II:
Directly in the .cpp file with extern "C" {header file file containing C library functions}
While doing the compilation:
g++ filename.cpp -I (path/to header file) -L (path/to library file) -l (library name)
5、What are the common suffixes for C++?
- .cpp
- .hpp
- .cc
- .h
- .cxx
6. What is the role of anonymous spaces in namespaces, and what are the similarities and differences with static in C?
The definition of anonymous spaces improves the speed of writing code, making the original std:: variables --->:: variables, which can be called directly
The anonymous space acts in the same way as Static in that local variables defined inside the calling function are not freed from memory as the function call ends, but rather the variables are not freed from memory until the entire program terminates.
The difference between the role of anonymous space and Static is that the scope of the static role is useful throughout the entire project file while the anonymous space is only used in the file where the anonymous space is defined.
7. The difference between references and pointers
Pointer: data type * pointer name = & variable
Reference : data type & reference name = variable (data type must be the same as variable type and must be initialized)
- Definitions and properties
Pointer: is a variable that stores the memory address of another variable. The pointer itself is aindependent entity,Has its own memory space。
Reference: is an alias for the original variable, which does notdoes not occupy a separate memory space, but shares the same memory address as the original variable。 - initialization
Pointer:Can be defined without initializing, but initialization is usually recommended to avoid pointing to uncertain memory addresses.
Citation:Must be initialized at definition time, and once initialized, it cannot point to another variable. - dexterity
Pointers: can point to other variables, including changing the pointing at runtime. Pointers can also have multiple levels, such as intp;。
References: once initialized, they cannot point to another variable. References can only be of the first degree, e.g. int &r = a;, and int &&a; (an attempt to define a second degree reference) is illegal. - empty value
Pointer:Can point to NULL(or nullptr in C++11 and later), indicating that it does not point to any object.
Citation:Cannot be NULL, it must always point to a valid object. - sizeof operation
Pointers: the sizeof operator returns theThe size of the memory occupied by the pointer itselfThis depends on the system architecture (e.g. 32-bit or 64-bit).
Quote: sizeof operatorReturns the size of the referenced object, rather than the size of the reference itself (since references do not occupy separate memory space). - (mathematical) operation
Pointers: support a variety of operations, such as arithmetic operations (addition and subtraction), comparison operations, etc. These operations are usually based on the memory address pointed to by the pointer.
Reference: does not support arithmetic operations, it is mainly used to access and modify the value of the variable it references. - Function parameters and return values
As a function parameter: pointer passes the address value, which is a kind of value transfer; while reference passes the alias of the variable, which can be regarded as a more intuitive and safer way of address transfer. When a reference is used as a function parameter, the value of the original variable can be accessed and modified directly without dereferencing.
As a function return value: functions can return references, allowing further operations on the returned object. While pointers as function return value, you need to pay attention to the validity of pointers and wild pointer problem. - safety
Pointers: Due to the flexibility of pointers, it may bring higher risks such as wild pointers, dangling pointers and other problems.
Reference: relatively safer, because once it has been initialized it cannot point to another variable, reducing the possibility of error.