Difference between revisions of "cpp/language"
(typo) |
(→Classes: +explicit) |
||
Line 127: | Line 127: | ||
* {{rl|friend | friend specifier}} grants access privileges to private/protected parts for non-member classes or functions | * {{rl|friend | friend specifier}} grants access privileges to private/protected parts for non-member classes or functions | ||
* {{rl|initializer_list | initializer lists}} | * {{rl|initializer_list | initializer lists}} | ||
− | * {{rl|virtual | virtual functions}} | + | * {{rl|virtual | virtual function specifier}} declares a virtual functions |
+ | * {{rl|explicit | explicit function specifier}} declares that a constructor or casting operator can not be used in implicit conversions | ||
====Special member functions==== | ====Special member functions==== |
Revision as of 12:25, 31 January 2012
Template:cpp/language/sidebar This is a brief reference of available C++ constructs.
Contents |
General topics
Preprocessor
Comments
Keywords
ASCII chart
Escape sequences
History of C++
Flow control
Conditional execution statements
Different code paths are executed according to the value of given expression
Iteration statements
The same code is executed several times
- for executes loop
- range-for executes loop over a range (since C++11)
- while executes loop, checking condition before each iteration
- do-while executes loop, checking condition after each iteration
Jump statements
Continue execution at a different location
- continue skips the remaining part of the enclosing loop body
- break terminates the enclosing loop
- goto continues execution in another location
- return terminates execution of the enclosing function
Functions
The same code can be reused at different locations in the program
- function declaration declares a function
- lambda function declaration declares a lambda function
- function template declares a function template
- inline specifier hints the compiler to insert a function's body directly into the calling code
- exception specifications enforces the function to throw only specific exceptions or not to throw at all (deprecated)
- noexcept specifier enforces the function not to throw any exceptions (since C++11)
Exceptions
Exceptions are a more robust way to signal error condition than function return codes or global error variables
- throw expression signals an error and transfers control to error handler
- try-catch block catches exceptions originating from specific block of code
Namespaces
Namespaces provide a way to prevent name clashes in large projects
Types
- fundamental types defines basic character, integer and floating point types
- compound types defines types, holding several data members (essentially the same as class)
- enumeration types defines types, that are able to hold only one of the specified values
- union types defines types, that can hold data in several representations
- function types define function call signatures, that is the types of arguments and the return type
- decltype specifier defines a type, equivalent to the type of an expression (since C++11)
Specifiers
- cv specifiers specifies constness and volatility of a type
- storage duration specifiers specifies storage duration of a type
- constexpr specifier specifies that the value of a variable or function can be computed at compile time (since C++11)
- auto specifier specifies that the actual type shall be defined from the expression, assigned to the variable (since C++11)
- alignas specifier specifies that the storage for the variable should be aligned by specific amount (since C++11)
Literals
Literals are the tokens of a C++ program that represent constant values, embedded in the source code.
- integer literals are decimal, octal, or hexadecimal numbers of integer type.
- character literals are individual characters of type Template:cpp, Template:cpp, Template:cpp, or Template:cpp.
- floating-point literals are values of type Template:cpp, Template:cpp, or Template:cpp
- string literals are sequences of characters, which may be narrow, multibyte, or wide.
- boolean literals are values of type Template:cpp, that is Template:cpp and Template:cpp
- nullptr is the pointer literal which specifies a null pointer value (since C++11)
- user-defined literals are constant values of user-specified type (since C++11)
Expressions
An expression is a sequence of operators and operands that specifies a computation. An expression can result in a value and can cause side effects.
- value categories (lvalue, rvalue, glvalue, prvalue, xvalue) classify expressions by their values.
- order of evaluation of arguments and subexpressions specified the order in which intermediate results are obtained.
- operators allow the use of syntax commonly found in mathematics
Common operators | ||||||
---|---|---|---|---|---|---|
assignment | increment decrement |
arithmetic | logical | comparison | member access |
other |
a = b |
++a |
+a |
!a |
a == b |
a[...] |
function call |
a(...) | ||||||
comma | ||||||
a, b | ||||||
conditional | ||||||
a ? b : c | ||||||
Special operators | ||||||
static_cast converts one type to another related type |
- operator precedence the order in which operators are bound to their arguments
- alternative representations alternative spellings for some of the operators
Utilities
- Types
- typedef declaration creates a synonym for a type
- type alias declaration creates a synonym for a type
- attributes defines additional information about variable (since C++11)
- Casts
- standard conversions implicit conversions from one type to another
-
const_cast
conversion -
static_cast
conversion -
dynamic_cast
conversion -
reinterpret_cast
conversion
- Memory allocation
- new expression allocates memory dynamically
- delete expression deallocates memory dynamically
Classes
Classes provide the concept of object-oriented programming in C++
- class declaration
-
this
pointer links to the current instance of a class in member methods - access specifiers
- friend specifier grants access privileges to private/protected parts for non-member classes or functions
- initializer lists
- virtual function specifier declares a virtual functions
- explicit function specifier declares that a constructor or casting operator can not be used in implicit conversions
Special member functions
- default constructor initializes the object with default contents
- copy constructor initializes the object with the contents of another object
- move constructor initializes the object with the contents of other, temporary object, minimizing copying overhead (since C++11)
- assignment operator replaces the contents of the object with the contents of another object
- move assignment operator replaces the contents of the object with the contents of other, temporary object, minimizing copying overhead (since C++11)
- destructor releases claimed resources
Templates
Allows functions and classes to operate on generic types