C++ Flashcards
What is the Rule of Three in C++ and what standard was it introduced?
Guideline for writing classes to manage dynamically allocated resources. It states that if you need to modify the destructor, copy constructor, or copy assignment operator you should modify all three.
IWhat is the Rule of Five in C++ and what standard was it introduced?
The Rule of Five adds the Move Constructor and Move Assignment Operator to the Rule of Three. It was introduced in C++11.
What does the static keyword do in C++?
When static is used inside a function, the variable persists across function calls. It is only initialized once. When used with globals, that variable or function’s scope is limited to the file in which it is declared. In a class, it creates a class variable or function that is accessible by all instances of the class.
What does the volatile keyword do in C++?
Volatile indicates to the compiler that the variable’s value can change unexpectedly due to external circumstances like a GPIO pin. This ensures the compiler doesn’t optimize the variable in ways that depend on it being a constant.
What is a pure virtual function in C++?
A pure virtual function is declared in an abstract class and it has no implementation and must be overriden in child classes
What’s pass-by-reference versus pass-by-value?
Pass by value creates a deep copy of the argument. Modifying the variable in the function does not affect its value in the outer scope. Pass by reference allows a function to modify the value of the original variable that is passed in as an argument
What’s the difference between pass-by-reference and pass-by-pointer?
They both allow modifying the original variable inside a function call, they both avoid copying. The difference is in the implementation of pointers and references. Pointers can be null, references must always refer to a valid object. Pointers can be reassigned, references cannot. References do not need manual memory management whereas pointers do if they point to dynamically allocated memory
What’s the difference between Shallow and Deep copying?
Shallow copying has both the new and original object point to the same place in memory. Deep copying gives the new object is own memory.
How can shallow copy issues be avoided with classes in C++?
Shallow copying occurs when both the original object and its copy point at the same block of memory. The default copy constructor and copy operator use shallow copies. They must be overridden to give the new class its own memory.
What are the three types of Smart Pointers?
Unique, Shared,
What’s the difference between a process and a thread?
A process is a running program. It has its own address space and is managed by the OS. Communication between processes requires IPCs like pipes, sockets, and message queues. Threads in a process share an address space and can share data. Their order of operation is determined by a scheduler.
What’s the difference between a mutex and a lock?
Mutexes are basic and have two states: locked and unlocked. Locks encapsulate mutex usage. Lock_guards acquire a mutex in the constructor and ensure its released when the lock_guard goes out of scope.
What’s are new and delete in C++?
the new operator returns a pointer to newly allocated memory. Delete takes a ptr to memory and deallocates it. To delete an array you must use delete[]. It’s recommended to use smart pointers instead of new and delete in modern C++.
What is an Rvalue in C++?
An RValue is a temporary expression which is used to initialize objects or compute the value of an operand. I.e. in i = 42, 42 is an Rvalue. Rvalues don’t have a directly accessible address.
What is an Lvalue in C++?
LValues have addresses that can be accessed. I.e. in i = 42, i is an lvalue.