C ++ 2.5.1 Processing Type: type alias

Article Directory

Type alias

Conventional manner using typedef, cpp11, may be used using

typedef int mi; //int 别名 mi
mi xa = 88;
using mii = mi; //mi别名mii
mii xb = 88;
cout << (xa == xb) << endl; //output 1

Pointer type alias

typedef char* ps; //指向 char 的指针; char* 的别名 ps;
char xx = 'X';
const ps str = &xx; //指向 char的 常量指针
*str = 'F'; //地址指向值可变
//str = &zz; //常量指针的地址不可变,本句会 error
cout << *str << endl;

If the char * ps representatives, substituted into the expression const ps str = &xx;will be const char* str = &xx;
(after the write pointer close type or variables before, have no effect)
on a study of this form, is a constant pointer, the underlying const, can not be by *指针变量assignment. In fact here is assigned. And can not change the address of a pointer variable. This is indeed in line pointer constant, that is the top level const "Can not change the variable itself (address)" features a.

const ps *p; //常量指针的指针
p = &str;
cout << **p << endl; //输出 F

//既然 p 是常量指针的指针,重点是,它就是一个指针,可以修改地址,指向一个 char(常量)的地址
char zz = 'Z';
char * q = &zz;  //这里改成常量指针形式 char *const q = &zz; 也是一样的结果
p = &q;
cout << **p << endl; //输出 Z

This pointer type alias is simply a pit.

Published 400 original articles · won praise 364 · Views 1.62 million +

Guess you like

Origin blog.csdn.net/jjwwmlp456/article/details/104046848