文章目录提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
- 前言
- 1. 关键字
- const
- ?constexpr
- typedef
- volatile
- 运算符
- 算数
- 关系
- 位运算符
- 逻辑运算符
- 赋值运算符
- 其他运算符
- 运算符优先级
- 函数
- 数组
- 函数修饰符extern static inline
- 函数重载
- ?可变参数函数
- 函数指针
- ?尾置返回类型
- 命名空间
前言
来自iLearning C++语言实战进阶 第一章 基础语法
1. 关键字 constint *const p; //指针是常量,值可以改变 const int p; const int *p;//指针的值可以改变,指向的值是常量不可以改变 const int *const p;//?constexpr
编译时优化,运行性能会比const好
?常量表达式不能包含函数调用或对象构造
?constexpr函数的返回值不能是void类型,不能声明新变量或者新类型
int *const p; //指针是常量,值可以改变 const int p; const int *p;//指针的值可以改变,指向的值是常量不可以改变 const int *const p;//typedef
类型别名
typedef int* GLAPIP; //指针 int a=3; GLAPIP b=&a;// int* b = &a;volatile
告诉编译器这是易变量,每次都需要从内存地址读取这个数据
volatile int i = 10; int a = i;//10 //中间用编译器不知道的方法修改i=32; int b = i;//32
如果没有volatile,编译器会直接令b=10;
运算符 算数+ - * / % ++ --关系
== != > < <= >=位运算符
& 按位与 | 按位或 ^ 按位异与,如果两个相应bit位相同,则结果为0,否则为1。0110^0101=0011 ~ 按位非 <<左移,乘以2的n次方 >>右移,除以2的n次方逻辑运算符
&& 与 || 或 !按位非赋值运算符
= += -= *= /= %= <<= >>= &= 按位与赋值 ^= 按位异或赋值 != 按位非赋值其他运算符
sizeof 三元运算符 ? : 访问结构体对象成员 . 访问结构体指针对象成员 -> 取地址 & 对应地址变量 *运算符优先级
括号 作用域
自增 逻辑非 一元运算符
算术
关系
位运算
逻辑
赋值
int a = 12,b=0; a+=b*=a-=b/=a-b://12 a+=b*=a-=b/=12函数 数组
void myfunc(const int *) void myfunc(const int [])函数修饰符extern static inline
extern
函数或变量已在别处定义
函数默认是extern 的
//head.h extern int i; extern int test();
//head.cpp
int i;
int test(){return 10;}
//main.cpp #includeextern int i; extern int test(); int main(){ std::cout< static
函数只能在本文件调用
其他文件可以定义同名的函数inline
函数重载
编译器在调用点展开函数,减少调用开销,提高执行效率,
与宏定义相比有类型检查,更安全优先级:
?可变参数函数 函数指针 ?尾置返回类型 命名空间
相同
const
类型提升
算法类型转换或指针转换
类类型转换namespce name{函数定义}
在命名空间以外使用name::多层命名空间嵌套
命名空间别名
namespace long = AVeryLongName;using namespace std;//实际不建议
匿名命名空间 只在本文件使用



