1.关键字是C++系统中预先保留的标识符单词,我们在定义变量或常量时不能使用关键字。
这是所有C ++关键字的列表。(从C ++ 17开始)
alignas | decltype | namespace | struct |
alignof | default | new | switch |
and | delete | noexcept | template |
and_eq | do | not | this |
asm | double | not_eq | thread_local |
auto | dynamic_cast | nullptr | throw |
bitand | else | operator | true |
bitor | enum | or | try |
bool | explicit | or_eq | typedef |
break | export | private | typeid |
case | extern | protected | typename |
catch | false | public | union |
char | float | register | unsigned |
char16_t | for | reinterpret_cast | using |
char32_t | friend | return | virtual |
class | goto | short | void |
compl | if | signed | volatile |
const | inline | sizeof | wchar_t |
constexpr | int | static | while |
const_cast | long | static_assert | xor |
continue | mutable | static_cast | xor_eq |
以下是一些常见的C++关键字及含义:
int
:声明一个整数类型的变量。void
:表示函数没有返回值或类型为空。return
:用于从函数返回一个值或结束函数执行。if
:用于条件分支。for
:用于循环结构。class
:用于定义一个类。this
:指向当前对象的指针。new
:用于动态分配内存。delete
:用于释放动态分配的内存。const
:用于声明常量或指定成员函数不修改对象状态。typedef
:用于创建类型别名。static
:用于声明静态变量或函数。volatile
:用于指定变量的值可能在任何时候改变。explicit
:用于构造函数,防止隐式转换。friend
:用于声明友元函数或友元类,允许它们访问私有和保护成员。inline
:建议编译器将函数内联以提高性能。namespace
:用于组织代码,避免命名冲突。using
:用于导入命名空间中的标识符,或者声明类型别名。
2.命名规则:①不能是关键字 ②只能由字母、数字、下划线组成 ③第一个字符必须是字母或下划线 ④标识符中字母区分大小写