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