C++ 语言
来自cppreference.com
< cpp
![]() |
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
这是一份简洁但可用的C++语言结构参考。
目录 |
[编辑] 基本概念
[编辑] 预处理器
[编辑] 注释
[编辑] 关键字
[编辑] ASCII码表
[编辑] 转义序列
[编辑] C++历史
[编辑] 程序流控制
[编辑] 条件执行语句
条件语句根据给定表达式的值执行不同的代码路径。
[编辑] 迭代语句
迭代语句多次执行一个代码路径。
原文:
Iteration statements execute a code path multiple times.
[编辑] 跳转语句
跳转语句使程序在一个不同的位置继续执行.
[编辑] 函数
可以在程序中的不同的位置重复使用的相同代码。
原文:
The same code can be reused at different locations in the program.
- 函数声明 声明函数原文:function declarations declare functions
- 函数模板 声明函数模板原文:function templatesdeclare function templates
- 内联限定符提示编译器将函数体直接插入到调用代码处原文:inline specifier hints that the compiler insert a function's body directly into the calling code
- 异常说明说明该函数只抛出特定的异常(已弃用)
- noexcept说明声明一个函数是否会抛出任何异常(C++11 起)
[编辑] 异常
异常是一个比函数返回值或全局错误变量更强大的通知故障状态的方式。
- throw表达式 通知错误并将控制权转移给错误处理程序。
- try-catch块 从特定的代码块中捕获异常。
- noexcept修饰符和 noexcept操作符定义和测试表达式是否抛出异常 (C++11 起)
[编辑] 命名空间
命名空间提供了一种在大型项目中避免名称冲突的方法。
- 命名空间声明 声明命名空间原文:namespace declarationsdeclare namespaces
- 命名空间别名 为已经存在的命名空间声明一个可替代的名称。
[编辑] 类型
- 基本类型 定义基本的字符型,整型和浮点类型原文:fundamental types define basic character, integer and floating point types
- 指针类型 定义一个存储内存地址的类型
- compound types 定义包含数个数据成员(本质上是属于相同的“类别”)的类型
- 枚举类型定义类型能够保持只有一个指定的值原文:enumeration types define types that are able to hold only one of the specified values
- 联合类型定义,可容纳数表示的数据的类型原文:union types define types that can hold data in several representations
- 函数类型定义参数和返回值类型的函数调用签名
- decltype说明相当于一个表达式(C++11 起)类型定义了一个类型原文:decltype specifier defines a type equivalent to the type of an expression (C++11 起)
[编辑] 限定符
- cv限定符指定类型的常量性和波动性原文:cv specifiers specify constness and volatility of types
- 存储时间说明符指定类型的存储时间原文:storage duration specifiers specify storage duration of types
- constexpr说明指定的变量或函数的值可以在编译时(C++11 起)计算原文:constexpr specifier specifies that the value of a variable or function can be computed at compile time (C++11 起)
- 自动符指定的表达,分配给变量(C++11 起)的实际类型应定义原文:auto specifier specifies that the actual type shall be defined from the expression, assigned to the variable (C++11 起)
- alignas说明指定的变量的存储空间应保持一致的具体数额(C++11 起)原文:alignas specifier specifies that the storage for the variable should be aligned by specific amount (C++11 起)
[编辑] 初始化
每当命名的变量的声明,每当创建一个临时对象,新对象的初始值是通过以下机制之一
原文:
Whenever a named variable is declared, and whenever a temporary object is created, the initial value of the new object is provided through one of the following mechanisms:
- 默认的初始化发生时没有初始化原文:default initialization occurs when no initializer is provided
- 值初始化发生时的初始值是一个空的括号原文:value initialization occurs when the initializer is an empty set of parentheses
- 零初始化 初始化的对象的每一个位到零原文:zero initialization initializes every bit of the object to zero
- 复制初始化 初始化一个对象从另一个对象原文:copy initialization initializes an object from another object
- 直接初始化 提供初始值或构造函数的参数列表中,在括号中原文:direct initialization provides the initial value or the list of constructor arguments, in parentheses
- 集合初始化 没有构造一个数组或结构的每一个成员提供初始值原文:aggregate initialization provides initial values to every member of an array or a struct without a constructor
- 表初始化 提供了一个任意长度的值列表,它可以初始化一个std::vector或std::map(C++11 起)原文:list initialization provides an arbitrary long list of values, which can initialize a std::vector or std::map (C++11 起)
- 常数初始化 恒定的静态对象初始化之前任何东西原文:constant initialization initializes all constant static objects before anything else
- 参考电压的初始化 绑定对象的引用,延长寿命临时原文:reference initialization binds references to objects and extend the lifetimes of the temporaries
[编辑] 常值
字面量是一个C++程序中在源代码中嵌入的,代表恒定值的标记.
原文:
Literals are the tokens of a C++ program that represent constant values, embedded in the source code.
- 整数字面量 是整数类型的十进制,八进制或十六进制数字。
- 字符字面量 是类型为char,char16_t,char32_t,或wchar_t的单个字符。
- 浮点字面量 是类型为float,double,或long double的值。
- 字符串字面量 是由窄字符、多字节字符、或宽字符组成的字符序列。
- 布尔字面量 是类型为bool的数值,其值是true或false。
- nullptr 是一个指代空指针值的指针字面量(C++11 起)。
- 用户自定义字面量 用户指定类型的常数值(C++11 起)。
[编辑] 表达式
表达式是用于表示一个计算的一系列运算符和操作数的序列。一个表达式可以产生一个结果,并产生一系列操作。
- 值类型(左值,右值,glvalue,prvalue,xvalue)根据它们的值对表达式分类原文:value categories (lvalue, rvalue, glvalue, prvalue, xvalue) classify expressions by their values
- 参数和子表达式的计算顺序指定了取得中间结果的顺序。
- operators允许使用数学中常见的语法。
Common operators | ||||||
---|---|---|---|---|---|---|
分配 | incrementNJdecrement | 算术 | 合乎逻辑的 | 比较 | memberNJaccess | 其他 |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Special operators | ||||||
- 运算符的优先级 定义运算符和参数绑定的顺序原文:operator precedence defines the order in which operators are bound to their arguments
- 另一种表示形式 一些运算符的其他拼写形式原文:alternative representations are alternative spellings for some operators
[编辑] 实用工具
类型
- typedef声明创建同义词的类型原文:typedef declarations create synonyms for types
- 键入别名报关单创建同义词的类型原文:type alias declarations create synonyms for types
强制转换
- 标准的转换的隐式转换为另一种类型原文:standard conversions implicit conversions from one type to another
-
const_cast
转换 -
static_cast
转换 -
dynamic_cast
转换 -
reinterpret_cast
转换 - 显式转换转换使用C风格的转换符号和功能符号原文:explicit cast conversion using C-style cast notation and functional notation
内存分配
- 新的表达动态分配内存原文:new expression allocates memory dynamically
- 删除的表情动态地释放内存原文:delete expression deallocates memory dynamically
[编辑] 类
类提供了面向对象编程的概念在C + +
原文:
Classes provide the concept of object-oriented programming in C++.
- 类的声明声明类
-
this
指针的当前实例中的类成员方法原文:this
pointer links to the current instance of a class in member methods - 访问符确定类成员的可见性原文:access specifiers determine visibility of class members
- 朋友说明授予访问权限的私有/受保护的部分非会员的类或函数原文:friend specifier grants access privileges to private/protected parts for non-member classes or functions
- 初始化列表初始化类成员的数据原文:initializer lists initialize class member data
[编辑] 类特定的功能特性
- 虚函数的说明声明一个函数是虚拟的原文:virtual function specifier declares that a function is virtual
- 覆盖说明声明一个虚函数重写另一个虚拟function.(C++11 起)原文:override specifier declares that a virtual function overrides another virtual function.(C++11 起)
- 最后说明声明一个虚函数不能被覆盖一个继承class.(C++11 起)中原文:final specifier declares that a virtual function can not be overridden in a inheriting class.(C++11 起)
- 明确的功能说明声明构造函数或转换运算符不能用于隐式转换(C++11 起)原文:explicit function specifier declares that a constructor or conversion operator can not be used in implicit conversions (C++11 起)
- 静态函数说明声明一个函数不使用类的数据原文:static function specifier declares that a function does not use class data
- CV功能说明声明一个成员函数只能用于品种限定对象原文:cv function specifier declares that a member function can only be used on cv qualified objects
[编辑] 特殊的成员函数
- 默认构造函数 初始化的对象默认内容原文:default constructor initializes the object with default contents
- copy constructor初始化的对象与另一个对象的内容原文:copy constructor initializes the object with the contents of another object
- 移动的构造函数初始化其他临时对象的内容的对象,最大限度地减少复制开销(C++11 起)原文:move constructor initializes the object with the contents of other, temporary object, minimizing copying overhead (C++11 起)
- 赋值运算符替换的对象与另一个对象的内容的内容原文:assignment operator replaces the contents of the object with the contents of another object
- 移动赋值运算符替换其他临时对象的内容的对象的内容,最大限度地减少复制开销(C++11 起)原文:move assignment operator replaces the contents of the object with the contents of other, temporary object, minimizing copying overhead (C++11 起)
- 析构函数版本声称资源原文:destructor releases claimed resources
[编辑] 模板
允许函数和类,泛型类型的操作
原文:
Allows functions and classes to operate on generic types
- 类模板声明声明一个类模板原文:class template declaration declares a class template
- 函数模板的声明声明一个函数模板原文:function template declaration declares a function template
- 模板特化定义了一个现有的用于特定类型的模板原文:template specialization defines an existing template for a specific type
[编辑] 优化
- 因为如果规则 可以不改变任何代码转换的输出原文:The as-if rule allows any code transformation that doesn't change the output
- 复制省略 ,包括RVO和NRVO,使传递的值在许多情况下,首选的方法.原文:Copy elision, including RVO and NRVO, makes pass-by-value the preferred approach in many situations.
- 空基类优化 多重继承,接口或策略类的开销,并须为标准布局类型.原文:Empty base optimization makes multiple inheritance from interfaces or policy classes overhead-free and is required for standard-layout types.
[编辑] 杂项
- 内联汇编允许使用的汇编代码,,沿着C + +代码原文:Inline assembly allows the use of assembly code alongside C++ code