Rico's Nerd Cluster

「离开世界之前 一切都是过程」

C++ - Streams

Stringstream, iostream, string

Streams fstream Open a file, then start appending to the file: 1 std::ofstream ofs(filename, std::ios::out | std::ios::app); How to add a custom print function? 1 2 3 4 5 inline std::...

C++ - chrono

Basic Timing: high-resolution clock 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <chrono> void foo(){ auto start = std::chrono::high_resolution_clock::now(); std::cout<<"Hello...

C++ - Manual Memory Garbage Collector

Boehm-Demers-Weiser (BDW) Collector

Boehm-Demers-Weiser Collector (BDW) Collector BDW Collector is designed as a “drop-in” replacement in C for the default memory allocation functions (malloc, free, etc.). Developers can link agains...

C++ - Iterators

Basic Usage An iterator needs to be dereferenced *it or it->first so its internal data can be accessed. Iterator Category Examples Supported Operations ...

C++ - [OOP] Members

Member Attributes and Methods, Copy, Move, Static Members

Member Attributes Let’s take a look at a tricky example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include <iostream>...

C++ - [OOP] Polymorphism Substitute - Curiously Recurring Template Pattern (CRTP)

static polymorphism

Optimization Impact: ⭐️⭐️⚪⚪⚪ CRTP is a design pattern where the base class takes in derived class as a template parameter. This way, no virtual function is created, no runtime cost is spent on vta...

C++ - [OOP] Polymorphism - Virtual Functions and Virtual Inheritance

Virtual is virtually complicated. Dynamic Dispatch, Dreadful Diamond Derivative (DDD) Problem ...

Introduction The virutal keyword is very versatile. A really short and nasty summary is, it is to ensure the correct functions or base class is inherited / loaded during runtime. Motivating Examp...

C++ - [OOP] Initialization 2 - Ordering

Default Initialization, Memory Order, Ctor for Inheritance, Parsing and Construction

Default Initialization Default Initialization is when a variable is initialized without explicit value. They could be in an uninitialized state and accessing them could be an undefined behavior. ...

C++ - [OOP] Initialization 1 - Construction Basics

Conversion, Assignment, Copy

Constructor And Assignment When an new object is created, it’s either: Constructed from scratch Or copied from another object. The object could be temporary or permanent. Conversion and Ex...

C++ - Friends

Friend Class, Friend Function, Inner Class

In C++, friend functions and friend classes provide controlled access to private members of a class. While useful, they should be used judiciously to maintain encapsulation and avoid unnecessary de...