[C++] Enhancement of C++ language to C language② (variable detection enhancement - C++ does not allow repeated definition of variables | struct keyword enhancement - struct is equivalent to class)





1. Variable detection enhancement - C++ does not allow repeated definition of variables



In the early C language compiler version , multiple global variables with the same name can be defined repeatedly, and the compiler will link these global variables with the same name to the same address space in the global data area;

For example: In one header file, a variable is defined int a;, and in another header file, a variable is defined int a = 1;, and the int type variable named ais defined twice;

When the program is running, calling the variable a actually accesses the variables in the same address space of the global data area, and the int a;addresses of the two variables are the same;

The following code can be compiled and run in the early C language;

#include <stdio.h>

int a = 10;
int a = 20;

int main()
{
    
    
	printf("a = %d \n", a);
	return 0;
}

In the current version of the C language compiler , it does not support repeated definition of variables; try to use gcc to compile and run the above code, and an error will also be reported;

C:\Users\octop\Desktop>gcc hello.c
hello.c:4:5: error: redefinition of 'a'
 int a = 20;
     ^
hello.c:3:5: note: previous definition of 'a' was here
 int a = 10;

insert image description here



In the C++ language, the same variable is not allowed to be defined repeatedly; if the variable is defined repeatedly in the C++ code, an error will be reported;

1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1>Hello.cpp
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(4,5): error C2374: “a”: 重定义;多次初始化
1>Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Hello.cpp(3): message : 参见“a”的声明
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0==========

insert image description here





2. struct keyword enhancement - struct is equivalent to class



The C language compiler does not consider the structure defined by the struct keyword to be a new type, but treats the structure as a set of variables; in the C language, the struct keyword is used to define an ordinary structure type , which It defines a fixed-size memory block alias , and no memory has been allocated to the structure at this time;

  • After the structure type variable is declared, the memory will be allocated in the stack memory ;
  • Use malloccan allocate memory for it in heap memory ;
/**
 * @brief The Student struct
 * 定义 结构体 数据类型 , 这是定义了 固定大小内存块别名
 * 此时还没有给 结构体 分配内存
 * 声明了 结构体类型 变量后 , 才会在 栈内存中为其分配内存
 * 使用 malloc 可以在堆内存中为其分配内存
 */
struct Student
{
    
    
    char name[20];
    int age;
    int id;
};

Declare the structure variable corresponding to the above structure type:

    // 在栈内存中 定义 Student 结构体 类型变量
    struct Student s1;

If you use Student s1;to define the above structure variable, an error will be reported, because the C language does not think that Student is a type;



In C++, the structure defined by struct is treated as a new type;

In C++ language, the struct keyword has the same function as the class keyword;

struct can also add public, protected, private access qualifier keywords;


In the C++ code, use the following code to define the structure Student, and use to Student s1;define the type variable, the following code is executable;

Code example:

#include <stdio.h>

struct Student
{
    
    
    char name[20];
    int age;
    int id;
};

int main()
{
    
    
    // 在栈内存中 定义 Student 结构体 类型变量
    Student s1;
    s1.age = 18;
	printf("s1.age = %d \n", s1.age);
	return 0;
}

Results of the :

s1.age = 18

Y:\002_WorkSpace\002_VS\HelloWorld\HelloWorld\Debug\HelloWorld.exe (进程 17180)已退出,代码为 0
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/132368329