ca4318,随便找电影网,新密传官网
数据类型
为一个已经存在的类型重新定义一个名称
#include <iostream> using namespace std; int main(){ typedef int hello; hello a = 10; cout << a; return 0; }
将int类型冲洗定义一个名称 hello,只是起了一个新的名字原来的还是可以继续使用的
#include <iostream> using namespace std; int main(){ enum color {red,green=10,blue} c; c = blue; cout << c; return 0; }
如何为变量c赋的值不是enum中的值时则会报错
声明变量
extern可以置于变量或者函数前,以标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义
main.cpp
#include <iostream> using namespace std; extern int a; int main(){ cout << a; return 0; }
test.cpp
#include <iostream> using namespace std; int a = 20;
结果输出 20
定义常量
定义常量可以使用两种方式 #define const 两者的区别在于:
1.类型检查不同,#define 不会检查常量的类型而只是单纯的将常量替换为预先定义的值;const 定义常量时需要指定常量的类型
2.#deifne 定义的常量可以使用 #undef 来取消,但是const定义的常量不能更改
3.定义域不同 #define 定义好的常量不受定义域限制 const 定义的常量有定义域的限制
#include <iostream> using namespace std; void declam(){ #define count 20 const int sum = 10; } int main(){ cout << count; cout << sum <<endl; //error: 'sum' was not declared in this scope return 0; }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论