1 枚举
1 enum ShapeType
2 {
3 circle,
4 square,
5 rectangle
6 };
7
8 int main() {
9
10 ShapeType shape = circle;
11
12 switch(shape)
13 {
14 case circle:
15 cout<<"ShapeType.circle"<<endl;
16 break;
17 case square:
18 cout<<"ShapeType.square"<<endl;
19 break;
20 case rectangle:
21 cout<<"ShapeType.rectangle"<<endl;
22 break;
23 default:
24 cout<<"Unknown!"<<endl;
25 }
26
27
28
29 return 0;
30 };输出:
ShapeType.circle
2. const_cast, static_cast
const int i = 0;
/*
error C2440: 'const_cast' : cannot convert from 'const int' to 'int'
Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
转换是一个合法的标准转换,可以通过隐式执行,或使用static_cast、C风格的转换、函数式转换进行
*/
//int j = const_cast<int>(i);
/* 从 const int --> int, 下面4中都可以,唯独const_cast常量转换方式不行,只是因为它是一个合法的标准转换,用简单的方式已能处理还用不到高级的const_cast常量转换 */
int j = i; //隐式转换
int h = static_cast<int>(i); //static_cast静态转换
int k = (int)i; //C风格转换
int t = int(i); //函数式转换
/*
const int * --> int * 使用const_cast常量转换
*/
int* s = const_cast<int *>(&i);
/*
error C2440: 'static_cast' : cannot convert from 'const int *' to 'int *'
Conversion loses qualifiers
*/
int* w = static_cast<int *>(&i);
//long* l = const_cast<long *>(&i); // error C2440: 'const_cast' : cannot convert from 'const int *' to 'long *'
const_cast的用法:
返回该常量对应的变量 = const_cast<该常量所属的类型>(常量)
const int * --> int * 使用 const_cast
const int i = 0;
/* error C2440: 'initializing' : cannot convert from 'const int *' to 'int *' */
//int* p = &i;
int* p = const_cast<int *>(&i);
const int --> int 是一个合法的标准转换,const int直接就可以隐含转换到int, eg. int i = 5;
3 union联合体
1 union Packed
2 {
3 char i;
4 short j;
5 int k;
6 long l;
7 float f;
8 double d; //该union的大小为double,8个字节
9 }; //分号是必须的,用于结束该union的定义
10
11
12
13 int main() {
14
15
16 cout<<"sizeof(Packed) = "<<sizeof(Packed)<<endl;
17
18 /*
19 * 联合体,用同一个变量,处理不同的数据类型
20 */
21 Packed p;
22
23 p.i = 'C';
24 cout<<p.i<<endl;
25
26 p.d = 3.14159;
27 cout<<p.d<<endl; //此时联合体变量p保存的是d成员的值
28
29 return 0;
30 };
4 数组
int a[10];
创建了10个存储单元连续的int型变量,但每一个变量都没有单独的标识符。相反,它们都集结在名字a下
int a[10]; //数组名a的类型信息为int *
const int *p = a; //指针变量p的类型信息为int const *
cout<<"a typeinfo: "<<typeid(a).name()<<endl; //int *
cout<<"p typeinfo: "<<typeid(p).name()<<endl; //int const *
cout<<"sizeof(int) = "<<sizeof(int)<<endl;
cout<<"a = "<<(long) a<<endl; //数组名的值
for (int i=0; i<10; i++)
cout<<"&a["<<i<<"] = "<<(long) &a[i]<<endl;
int型量占用4个字节,数组a占用的内存单元从176到212+4,共40个字节
数组名a的值,是第一元素a[0]的地址, a的值 与 &a[0] 相等
想给一个函数传递数组
1)声明一个数组作为函数参数
2)声明一个指向数组元素的指针
1 void func1(int a[], int size)
2 {
3 for (int i=0; i<size; i++)
4 a[i] = i*i -i;
5 }
6
7 void func2(int* a, int size)
8 {
9 for (int i=0; i<size; i++)
10 a[i] = i*i +i;
11 }
12
13 void print(int a[], string name, int size)
14 {
15 for (int i=0; i<size; i++)
16 cout << name << "[" << i << "] = " << a[i] <<endl;
17 cout<<"--------------------------"<<endl;
18 }
19
20
21
22 int main() {
23
24 int a[5];
25
26 //数组未初始化,输出的是一些无意义的值 -- 把数组a作为参数传给print函数,通过指针直接在同一块内容单元上操作,修改有效
27 print(a, "a", 5);
28
29 //初始化数组,并输出
30 func1(a, 5);
31 print(a, "a", 5);
32
33 //修改数组元素的值,并输出
34 func2(a, 5);
35 print(a, "a", 5);
36
37
38 return 0;
39 };
命令行参数字符指针数组
命令行中每一个用空格分隔的字符串都被转换为一个单独的命令行参数
int main(int argc, char* argv[])
{
cout<<"argc = "<<argc<<endl; //命令行参数的个数
for (int i=0; i<argc; i++)
cout<< "argv[" << i << "] = " << argv[i] <<endl;
return 0;
};
字符指针数组,即指向字符型的指针的数组,数组中的每个元素都是指向字符型的指针
5 函数指针
void func(int x)
{
cout<<"func("<< x <<") called ... "<<endl;
}
int main(int argc, char* argv[])
{
void (*fp)(int); //定义函数指针fp, 该函数有1个int参数,无返回值
fp = func;
(*fp)(2); //func(2) called ...
void (*fp2)(int) = func; //定义函数指针fp, 并把函数func的地址赋给它
(*fp2)(3); //func(3) called ...
return 0;
};
原文链接: https://www.cnblogs.com/asnjudy/p/4540374.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/216865
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!