Nested Classes and RAII Flashcards
What is the first thing that is run when an instance of a class is created?
A class constructor
What are the two kind of special constructors in C++?
- Copy constructors
- Move constructors
What is a copy constructor?
These constructors take an existing instance of a class and “deep” copy its resources into a new instance
What is a move constructor?
These constructors take an existing instance of a class and takes its resources for a new instance so that the original class no longer has them.
What is the call for a copy instructor (class A)
A(const A& other) {};
What is the call for a move instructor (class A)
A(A&& other) {};
How can we achieve creating move and copy functions?
Using operator overload.
What is the function call for copy assignment?
A& operator=(const A& other) {};
What is the function call for move assignment?
A& operator=(A&& toher) {};
What do classes model?
Model semantics and responsibilities or objects and their control of resources.
What can semantics always be simplified down to?
The “atomic” level. Members of classes can be other any type.
We use abstraction to make our life easier.
How many times should you implement a type?
A typed should be implemented once, so that it can be used everywhere else we need it. We shouldn’t have to worry about how we use it if it is implemented correctly.
What does RAII stand for?
Resource Acquisition Is Initialisation
What is RAII?
A programming technique is C++, the idea is the lifecycle of a resource must be acquired before used and released when the object is destroyed (constructors and destructors).
When an object is initialised we must obtain its resources.
What order does resource release happen in?
Reverse order to acquisition (FILO)
What does RAII require you to craft?
Code that ensures safety and stability.
What is RAII in simple terms?
Writing our code in a way such that we acquire resources when initialising, and ensure that we release those resources at an appropriate time.
What are memory leaks?
When a resource is never released