CppClass
[toc]
class
- The fundamental ideas behind classes are
data abstraction
andencapsulation
. - The only difference between classes and structs is the default visibility.
- Use the prefix
m_
to denote a private variable. - class can reduces duplication
e.g.
1 |
|
Initializing a class
1 | Entity e("Randolfluo"); //stack |
Allocate on the heap:
- Object really really binggggg!
- I want to explicitly control the lifetime of the object.
Inheritance
1 |
|
Log class
1 |
|
class lifetime
1 |
|
constructor && destructor
Don’t forget initialization.
The constructor is automatically executed when the object is instantiated and does not need to be called manually.
- A class can have multiple constructors. Each constructor must differ from the others in the number or types of its parameters.
1 |
|
virtual function
- Base on Dynamic Dispatch && v table,cause performance loss.
- Like java’s overwrite
1 |
|
virtual destruction
- When the parent class pointer points to the child class, the correct fictitious function is called to prevent memory leaks
1 |
|
//TODO
Initialization List
- Initializers help you get initialization done quickly
1 |
|
->
1 |
|
1 |
|
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Randolfluo's blog!