C++ Flashcards
How should one always give arguments to a function if possible?
as const reference
one line if statement
“expression” ? “command if true” : “command if false”
what should you always check when creating new functions?
const correctness
what is a static member variable?
A variable which belongs not to the instance but to the class -> multiple instances access the same value
what is :: called?
scope resolution operator -> then searches global namespace
If you do not specify the access modifier in a class, are it’s members private, protected or public?
private
what is a std::map?
A container, that stores key-value pairs. Keys must be unique (otherwise the old entry is overwritten). A map is sorted through the keys through the < operator, which can also be provided.
what are std::vector and std::list?
both are containers
vector: elements in one block of memory, easy to add and remove elements at end, not so easy to insert, …
list: indirect adressing -> good for insertion, slower for iteration
what important std containers are there?
vector, list, map, multimap, queue, stack, deque, set, array, …
what is the extern keyword?
it says, that a variable is defined in another translation context
how does one define a C++ macro?
define “macroname” “code”
- not recommended in modern C++
How do I declare a variable that stores the address of another?
type* varA = &varB;
what is * called in C++? How can it be read?
Dereference operator
“value pointed to by”
what is & called in C++? How can it be read?
2 meanings:
1. Address-of operator when used in expression
“address of”
2. In declaration: part of identifier, denotes a reference
“is a reference to”
How does one declare a reference in C++?
“type” & “reference name” = var;
what is the difference between a pointer and a reference?
- reference is a “const pointer”. It is initialized at declaration and the addres it points to cannot be changed.
- it is dereferenced implicitly, so no * is needed
What is pass-by-value/reference?
a pass by value function takes an input object and makes a copy of it, thus not changing the objects value.
pass by reference takes a reference and thus operates on the original object.
What does the declaration of a pass-by-reference function look like? How is it called?
typeA func(typeB & a){…}
typeB b = …;
func(b)
When should objects be initialized? And why?
Always upon declaration to omit undefined behaviour
Should one initialize members as declaration in the constructor or in the initialization list?
Always in the initialization list
In a switch, if multiple statements shall execute the same code, how does one do it?
case a:
case b:
{
shared code…
} break;
what does mutable/immutable mean in C++?
immutable means not changeable (const). Mutable is a keyword that can be applied to a field of a const object, which reverts it’s immutability
which function converts const char to int? (e.g. for input args)
atoi()
what is the difference between
CParid testParid = getUpdatedParidFromFile();
and
CParid testParid;
testParid = getUpdatedParidFromFile();
the first uses the copy constructor, while the second uses the = operator
how does one define a lambda function?
“type” name = [“capture clause”] (“arguments”) -> “explicit return type (optional)”{ “function body”}
How can I define a function in a .h file?
By using the inline keyword. Otherwise the linker will load it multiple times if applicable
How is an R-value reference written
&&
When should a virtual destructor be used? And when not? why?
Polymorphic base classes should have a virtual destructor. Final classes should NOT!
If a polymorphic base class does not have a virtual destructor, only the child classes destructor is called upon deletion, leading to a memory leak (where the “base class part” does not get deleted)
If a final class has a virtual destructor (or any virtual function), additional overhead (virtual table) is created.
When creating an empty class, which methods are created automatically?
Constructor
Copy Constructor
Destructor
Copy Assignment Operator
When is no default copy constructor, destructor or copy assignment operator created?
If the logic doesn’t make sense. I.e. if a member is a reference, reassignment doesn’t work. Or if the class has a base class with private destructor, since the default destructor want’s to call the base’s
how does one declare a function, that takes a function as an argument?
type functionName(std::function<return type(input type)> name)
how can a function that takes an argument be called without arguments?
If default arguments are defined or it is overloaded
What is the difference between heap and stack memory? How are variables allocated there?
Stack: very quick, follows call stack. Value type initialization writes there. Deleted when scope ends (by moving memory pointer back)
Heap: accessible from outside of scope and not in order. Reference types (new keyword) a lot of overhead. Danger of garbage.
Always use stack if possible
Does static write variables into stack or heap?
Non of those. Static is it’s own memory segment
what is the difference between const_iterator and iterator?
const iterator doesn’t allow you to change the values it points to