1.C++关键字
C++总计63个关键字,C语言32个关键字
大概了解一下C++的关键字都有哪些
asm | do | if | return | try | continue |
auto | double | inline | short | typedef | for |
bool | dynamic_cast | int | signed | typeid | public |
break | else | long | sizeof | typename | throw |
case | enum | mutable | static | union | wchar_t |
catch | explicit | namespace | static_cast | unsigned | default |
char | export | new | struct | using | friend |
class | extern | operator | switch | virtual | register |
const | false | private | template | void | true |
const_cast | float | protected | this | volatile | while |
delete | goto | reinterpret_cast |
2. 命名空间
在C/C++中,变量、函数和后面要学到的类都是大量存在的,这些变量、函数和类的名称将都存在于全局作用域中,可能会导致很多冲突。使用命名空间的目的是对标识符的名称进行本地化,以避免命名冲突或名字污染,namespace关键字的出现就是针对这种问题的。
2.1 命名空间定义
定义命名空间,需要使用到namespace关键字,后面跟命名空间的名字,然后接一对{}即可,{}中即为命名空间的成员。
1.普通的命名空间
namespace N1 //N1为命名空间的内容
{
//命名空间中既可以定义变量,也可以定义函数
int a;
int add(int left, int right)
{
return left + right;
}
}
2.命名空间可以嵌套
namespace N2
{
int i;
double d;
int Add(int x, int y)
{
return x + y;
}
namespace N3
{
int a;
int b;
int Sub(int x, int y)
{
return x - y;
}
}
}
3. 同一个工程中允许存在多个相同名称的命名空间,编译器最后会合成到同一个命名空间中。
namespace N1
{
int a;
int add(int left, int right)
{
return left + right;
}
}
namespace N1
{
int Mul(int x, int y)
{
return x + y;
}
}