Special Member Function
Operator overloading
Dynamic Typing
Lambda
[] () {}
# ^ ^ ^
# | | | body
# | |input parameter
# |capture [=]: capture all variable by value
[&]: capture all variable by reference
[=, &x]: capture all by value, but x by reference
[&, =a, =b]: capture a and b by value, but others by reference
[y=2] (int x) { return x + y}: y is local to lambda, require C++ 14
std::vector<string> strings(5);
[vector_strings = std::move(strings)](){/**/}();
# strings now have 0 element, size = 0Warning
[this](){} # capture this by reference
Smart Pointer
cv qualification
constant qualification
volatile qualification
const_cast
concept
forward
Exceptions
Virtual Destructor
- If destructor is not virtual, static binding is used
- We need to used dynamic binding
- Destructor of base class must be virtual
- If no destructor is defined, the compiler will synthesis one which is NOT virtual
- If we are going through a pointer to base, this means that the derived part is not destroyed, this leads to:
- Memory Leak
- Resource Leak
- Undefined Behaviour
class Shape{
public:
virtual ~Shape() {/* */} // Must be virtual if there is the base class
}
class Circle : public Shape {
public:
~Circle(){/**/}
}Move Semantics
Info
Deleting nullptr has no effect
Misc
constexpr:
- introduced in C++11 and improved in C++14. It means constant expression.
- indicates that the value, or return value, is constant and, where possible, is computed at compile time.
- C++14 standard requires the types in constant expressions to be literal types
Literal Types
A literal type is one whose layout can be determined at compile time. The following are the literal types:
- void
- scalar types
- references
- Arrays of void, scalar types or references
- A class that has a trivial destructor, and one or more constexpr constructors that are not move or copy constructors. Additionally, all its non-static data members and base classes must be literal types and not volatile.
Ref: literal types