Guide
sizeof(array)
1 | int print_size1(int a[], int n) |
An array-type is implicitly converted into pointer type when you pass it in to a function.
int a[]作为函数参数,隐式的转换为int *a.
编译会产生warning.
warning: ‘sizeof’ on array function parameter ‘a’ will return size of ‘int*’ [-Wsizeof-array-argument]
std::cout<< sizeof(a) << std::endl; // pointer size
char* string
1 | void test_str1() |
char*-vs-stdstring
const string
NULL vs nullptr
nullptr
NULL (void *)0
- convert to integer
- pointer
nullptr keyword
- pointer
- CAN NOT convert to integer
- nullptr is convertible to bool.
const vs non-const
Use const whenever possible.
将某些东西声明为const可以帮助编译器侦测出错误的用法。const可以被施加于任何作用域内的对象、函数参数、函数返回类型、成员函数本体。
当const和non-const成员函数有实质等价的实现时,令non-const版本去调用const版本可以避免代码重复。反之则不可。
code
1 | class TextBlock { |
两次转型:
第一次,用来为this添加 const,将this从其原始类型TextBlock& 转换为const TextBlock&,使得接下来调用operator[]是可以条用const的版本,使用static_cast
。
第二次,则是从const operator[]的返回值中移除const,利用const_cast
来完成。
static
- static: internal linkage
- extern: external linkage
extern
extern is present by default with C functions.
Since the declaration can be done any number of times and definition can be done only once
volatile
volatile 异变的,告诉compiler这个值可能会在当前线程外部被改变,因此不要进行优化,每次都从ram地址读取,而不要从register读取缓存的副本。
Internal Linkage and External Linkage in C
internal linkage and external linkage
what-is-external-linkage-and-internal-linkage
scope is a property handled by compiler, whereas linkage is a property handled by linker.
external linkage means the symbol (function or global variable) is accessible throughout your program and internal linkage means that it’s only accessible in one translation unit.
You can explicitly control the linkage of a symbol by using the extern and static keywords. If the linkage isn’t specified then the default linkage is extern for non-const symbols and static (internal) for const symbols.
The keyword static plays a double role. (1) When used in the definitions of global variables, it specifies internal linkage. (2) When used in the definitions of the local variables, it specifies that the lifetime of the variable is going to be the duration of the program instead of being the duration of the function.
constexpr
constexpr is a feature added in C++ 11. The main idea is performance improvement of programs by doing computations at compile time rather than run time.
constexpr vs inline functions
Both are for performance improvements, inline functions are request to compiler to expand at compile time and save time of function call overheads. In inline functions, expressions are always evaluated at run time. constexpr is different, here expressions are evaluated at compile time.
vtable and vptr
virtual-functions-and-runtime-polymorphism
what-are-vtable-and-vptr
calling-virtual-methods-in-constructordestructor-in-cpp
It is highly recommended to avoid calling virtual methods from constructor/destructor.
virtual-function-table
class-memory-layout
Virtual Constructor
Virtual Constructor, NO
Can we make a class constructor virtual in C++ to create polymorphic objects? No. C++ being static typed (the purpose of RTTI is different) language, it is meaningless to the C++ compiler to create an object polymorphically. The compiler must be aware of the class type to create the object. In other words, what type of object to be created is a compile time decision from C++ compiler perspective. If we make constructor virtual, compiler flags an error.
Virtual Destructor
Deleting a derived class object using a pointer to a base class that has a non-virtual destructor results in undefined behavior.
Advanced
How to make a C++ class whose objects can only be dynamically allocated?
Is it possible to call constructor and destructor explicitly?
Yes, it is possible to call special member functions explicitly by programmer.
When the constructor is called explicitly the compiler creates a nameless temporary object and it is immediately destroyed.
we should never call destructor explicitly on local (automatic) object, because really bad results can be acquired by doing that.
Local objects are automatically destroyed by compiler when they go out of scope and this is the guarantee of C++ language.Print 1 to 100 in C++, without loop and recursion
Template Metaprogramming
thread
- pass by value by default
- pass by ref:
std::ref(variable)
Reference
History
- 20190429: created.