Please enable JavaScript.
Coggle requires JavaScript to display documents.
C++ - Coggle Diagram
C++
module 13 classes
-
-
-
-
-
-
-
constness of classes
const sets all members to const, and you can call only const member functions
-
-
Anon objects
is objects that can be used only in one statement, in which this object is created, used and then destroyed
-
-
module-11
arrays
static array, we can allocate space only with compile time variables
-
-
-
-
string and string_view
-
string view is independent of actual string, so if the string dies it will lead to undefined behavior
-
-
std array - fixed size, more comfortable to use than traditional c style array
-
iterators
like pointers they can be dangling, when this happens they are invalidated iterators, it will lead to UB
standard lib algorithm
3 types
inspectors
just view and not modify data, like searching and counting
mutetors
modifies data in containers, like sorting and shuffling
facilitators
generates result based on data in containers, like multiplying all elements
-
Module 4
-
-
-
Floating point
int x{5}; // 5 means integer
double y{5.0}; // 5.0 is a floating point literal (no suffix means double type by default)
float z{5.0f}; // 5.0 is a floating point literal, f suffix means float type
-
-
string view read only string, optimized for reading
module-8
type conversion
implicit type convertion
-
-
numeric promotion (char to int, short to int)
numeric conversion (int to short, char to short, double to int, double to float)
-
arithmetic conversion (using operators with different types you can get arithmetic conversion when one type converted to another)
if on priority list, result is converted to type that is higher on this list
-
explicit type conversion
type casting
c-style cast
uses other types of casting depending on context. Even uses const cast and reinterpret cast that we should avoid.
-
-
-
-
-
-
-
type deduction
auto keyword
-
auto uses the same rule as template type param, for function declaration
can deduce constness and pointiness (because it is part of type), but no ref
-
use cases
auto will never perform conversions, so it can be used to ease the pain of using long and int and uint and etc etc
-
-
trivial conversions
do not change value, but change type like int to const int
-
-
Module 6
-
-
-
Inline functions
is a way to optimize function calls, by replacing function with code
we musn't use inline keyword, because compiler decides whether func is inline or not (for optimization purposes)
Constexpr function
-
to be eligible for compile time evaluation, constexpr function mustn't call non constexpr functions, and parameters have to be constexpr or literals
-
-
compile time cannot be force, except constexp var assigning
-
-
module 12
function pointer
-
-
overall use std function or alias to declare fn pointers. Fn pointers are useful if we want to store functions in data structure or to pass as an argument
-
recursion
-
-
to optimized and make less func calls we can use memoization! This is technique that caches results of previous calls so we don't need to call again function
Command line arguments
args in main()
int argc
number of passed arguments, always at least 1 because first is the name of the program
char* argv[]
just c style array of string, each string is argument
-
Intro to lambdas
-
lambdas aren't actually functions, but functors, special objects. this is just shenanigans of cpp implementations
use std::function, func pointers and auto to store lambdas
in lambda context, auto keyword is shorthand for template parameter
-
Lambda captures
give access to variables that normally out of scope of lambda func, it is in this "[ ]" things
-
captured vars have the same name, but not the same type with origin
-
-
default captures, we don't need to enlist anything with this, compiler automatically enlist everything
-
dandling capture variables - captured variables have to outlive the lambda otherwise it will lead to Undefined Behavior
lambda is just an object, so it can be copied
-
Comments
1) At the library, program, or function level, use comments to describe what.
2) Inside the library, program, or function, use comments to describe how.
3) At the statement level, use comments to describe why.
Best practice:
Comment your code liberally, and write your comments as if speaking to someone who has no idea what the code does. Don’t assume you’ll remember why you made specific choices.
-
Module-7
-
assert and static assert
Precondition, invariants and postcondition
-
-
-
assert is for programmers (not production), conditions that mustn't be there, and error handling is for production where end user may encounter this kind of error.
-
-
assert
abort program, so if you have db connections just dont use it
mustn't have side effects, code must run with or without assert in the same way
-
module-9
Value Categories
expression properties
type
result of expression has type, and so this is type of expression too
type of expression is determent at compile time, but value can be determent either at compile time (constexpr) or runtime (not constexpr)
value
this property is what expression resolves to. For e.g. this expression resolves to value, function, or an object of some kind.
-
lvalue
-
-
-
lvalues expressions are those that evaluate to variables or other identifiable objects that persist beyond the end of the expression.
lvalue references
-
reference can be returned by function, BUT returned identifiable object has to outlive function return expression, otherwise it will lead to undefined behavior.
rvalue
-
-
rvalues expressions are those that evaluate to literals or the returned value of functions and operators that are discarded at the end of the expression.
Type deduction and const
type deduction (auto) drops top level const and pointers and reference by default, but not low level const
top level const is const that applied to object like int* const ptr, const int val
low level const is const that applied to object that is referenced to or pointed to like const int* ptr, const int& ref
-
-
How to design a program
Define your goal
Define requirements
Define you tools, target, backup plans
-
-
-
virtual function table is for late binding, it contains table of virtual functions for an object and calls child's implementation of function
-
-
-
-
-