1, Coding structures
- templates < inheritance < composition in terms of better usage frequency.
2, Helper class
- as a static - does not keep state of the class instance.
- as a singleton - keep a universal state of a single class instance.
- as a base class - distinct states and fine-grain control of accessibility to its helper functions.
3, Namespace
- Preventing redefinition of user-defined keyboards.
- A C++ framework should have at least one namespace to group all files.
- Compiler just add this constant string as prefix of variables.
4, Forward declarations
- Only used as pointer types.
- Behaving like void* but no need to cast.
5, Virtual inheritance
- To solve ambiguous hierarchy composition (known as the "diamond problem")
- A particular base class in an inheritance hierarchy is declared to share its member data instances with any other inclusions of that same base in further derived classes.
6, Class ID Macro
- AMF_DECLARE_IID("AMFCFilter")
- Macro methods for getting current class name for debugging.
7, x86 calling conventions
- Caller clean-up: cdecl syscall optlink
- Callee clean-up: stdcall fastcall register // AMF_STD_CALL informs compiler how to call.
8, Pure virtual functions
- It overwrites this method pointer in class instance memory to point to a warning methods. The program terminates if the pointer is not overwritten and output warning.
- Basically pure virtual functions are required to be implemented in sub classes.
9, Virtual functions
- Able to calling the corresponding virtual methods based on class instance memory instead of static pointer type.
- If B is a subclass of A: A* pA = new B(); pA->foo(); => invokes B's foo().
10, Call base class' overwritten methods from subclass.
- base_class::foo(...)
- Unlike Java/C#: C++ does not have super or base keywords due to support of multiple inheritance.
11, String
- wchar_t: 2 bytes per char unicode - support minimum Chinese, Korean, etc.
- ANSI: 1 byte per char
12, Single/ Double/ Triple, etc. pointers
- A *pA - dereference 1 time to get value.
- A **pA - dereference 2 times to get value.
- A ****pA - dereference 4 times to get value.
13, Synonyms
- int i = 0; int &j = i; compiler will replace j with i during compilation.
- Compare to pointer, reference could control size of an array. foo(int[4] &pA);
14, static_cast vs reinterpret_cast vs const_cast vs dynamic_cast
- static_cast is a strict explicit casting. (Make sure casting fine at compile time.)
- reinterpret_cast is a loose explicit C style casting. (You know what you doing or it fails.)
- const_cast converts const variable to be non-const variable. (Should not use unless its a hack.)
- dynamic_cast is the same as static_cast at compile time, but it also checks at runtime. (Never used)
15, Include guards
- #ifndef #define #endif - C style
- #pragma once - C++ style
16, Operator overloading
- TYPE& operator=()
- Only the above function signature matches then the overloaded methods is called.
17, Constructors vs Copy Constructors vs ...