The Knowledge - Modern C++ Flashcards
include
Predict the output of the following program:
int x{100};
int main() { std::cout << x << std::endl; int x{256}; std::cout << x << std::endl; { int x{128}; std::cout << x << std::endl; } }
See page 16 of the manual. In general, the inner declaration overrules the outer declaration.
include
Given the following program:
int x{1000};
int main() { int x{256}; }
How can you change the code such that I can do a cout of the global x?
The scope resolution operator can be used:
std::cout «_space;::x «_space;std::endl;
What are the three object lifetimes? Describe the lifetime of each one of them.
- Automatic objects: they exist from the point of defintion to the end of the closing block (function).
- Static objects: they exist from the point of definition to end of main() (entie run-time of the program). Static objects are allocated at compile time.
- Dynamic objects: from the “new” until the “delete” (the programmer needs to manually delete these objects!!)
What are the rules for the “internal linkage” of static global objects? Provide an example.
Applying the static keyword to an object defined outside of any block gives the object an internal linkage. (page 22).
What are the rules for STATIC AUTOMATIC obejcts?
What’s the advantage of using a static automatic object?
(page 23 manual)
How does the memory allocation of a program look like? Describe the different sections.
(page 25 of the program)
External objects: visible throughout the program lifetime.
Automatic objects: their scope is limited by the curly braces of where they are defined (e.g. a function)
Static objects: they have the scope of an automatic object, but the lifetime of an external object.
How does the memory allocation of a program look like? Describe the different sections.
What about data definitions?
(page 25-26 of the manual)
Explain some possible problems in the following code snippet:
int main() { int a{10}; unsigned int b{0xFFFFFFFF};
float f{0.0F}; double d{100.7}; a = b; f = d; a = f; }
What is a “best practice”? What would be better to do in this case?
page 35 of the manual.
In general, it is better to do something like:
a = static_cast(b); f = static_cast(d); a = static_cast(f);
Given the following structure:
struct Example {
int i;
char c;
};
How can I do a C++20 named initialization for a structure object?
How can I do an all-elements value-initialized?
How can I do a direct initialization?
int main() { Example ex4 { .i = 100, .c = 'x' }; }
========================= Example ex2{}; ========================= Example ex3 {100, 'x'}; ==========================
What is brace elision? Provide an example in which brace elision is a better choice.
For instance, given the following code snippet, how can I use brace elision?
struct Inner {
int arr[2];
};
strutct Outer {
Inner inner;
int other_data;
};
int main() { // Write code here }
Brace elisiion is a syntactic mechanism to simplify (in some cases) nested structures.
What’s the difference between
Position p1{};
And
Position p1;
?
If you forget the (empty) brace initialized your object will be uninitialized - the values of the attributes will be whatever is in the memory at the object’s location.
What is good practice for class members initialization?
Page 55: should you forget the brace initializer on the class, the defaults will still be applied.
What’s the difference between the brace initialization used for a structure and a class in C++?
What’s a “good practice guideline” for C++ classes?
For class types, unlinke structures, the brace initialization syntax is not interpreted as direct initialization of the members but as a call to a constructor function.
Coding guidelines: Prefer NSDMIs to writing constructors that initilise all members.
What’s the problem in the following code snippet:
=========================================
class Position {
public:
Position(double azi, double elev);
... private: doublle rho { 90.0 }; double theta { 90.0 }; };
int main() { Position p1 { 0.0, 10.6 }; Position p2 {}; } ==========================================
How can we make this code work?
The line:
Position p2 {};
will give an error since our constructors will disable the compiler-supplied default constructor.
How to fix the code: by adding
Position() = default;
This way, the compiler-supplied default constructor will be re-enabled.
Predict the output of the following code snippet:
==========================================
class Position {
public:
Position ( double azi = 45.0, double elev = 45.0 );
void printRho() { std::cout «_space;rho «_space;std::endl; }
void printTheta() { std::cout «_space;theta «_space;std::endl; }
private:
double rho { 90.0 };
double theta { 90.0 };
};
int main() { Position p1 {}; p1.printRho(); p1.printTheta(); } ============================================
What’s the best coding practice in this case?
- 0
- 0
Best coding practice: Always ensure that constructor parameter defaults are the same as the default initializers.
What are aggragate types in C++?
Inherited from C:
Arrays
Structures
Unions
What the keyword “explicit” in C++ be used for?
Think about this example:
======================================= class Interlock { public: explicit Interlock (bool lock_state); explicit Interlock ( int lock_state); private: bool locked { false }; };
void make_safe ( Interlock i1, Interlock i2 ) { // Do something.. }
int main() { Interlock lock1 { false}; Interlock lock2 = false; // ERROR! } ======================================
What would happen instead if the “explicit” keyword was not specified?
Page 82.
The explicit keyword allows explicit instantiation (and initialization) of objects, but not (implicit) creation of an object via a temporary object using a converting constructor.
How can we disable pass-by-value semantics for a specific class in modern C++? What about the assignment operator?
Page 84-85. We can declare the copy constructor as “delete”.
What’s the “Rule of the Big Three”?
When do you have to provide your own implementation of the 3s?
Page 86. Your own implementation is required if your class has to manage the lifetime of another object.
What are const functions and what they are needed for?
The const qualifier in the declaration informs the compiler that on the call to this function it will not change the state of the object.
What’s the main issue with returning objects by value?
What’s instead a good practice to apply? Provide an example.
Page 89.
Good practice: Return Value Optimization
What is copy elision and when it happens?
Page 92.
What is the best coding practice related to automatic type declaration? Why?
Page 93. Coding guideline:
Only use automatic type-deduction when the initialization expression is a function call (and that function call gives an indication of what will be generated).
What are the rules to use auto-deduced function return types? When would that be useful?
What’s the best coding practice in this case?
C++14: Avoid auto return-type deduction in non-template code, as it gives no information to client as to the object being returned.
- Provide an example about how to use std::tuple.
- How to cout the values from it?
- When using a std::tuple object would be useful?
- How can we assign values from a tuple object to some other local variables? Provide an example. How could this be different in C++ 17?
Page 95.
Build an example by using std::optional. In this case, you need to write a function to convert a string of characters to an integer. This may fail, so we want to return an optional result.
Page 99.
What are literals? How can we declare a sized or signed interger literal?
Page 105.
U, u: declares that the literal is an unsigned int.
L, l : Literal is a long int.
There is no short qualifier for literals.
What are literals? How can we declare a sized or signed interger literal? What is good practice?
What’s wrong in the following code snippet:
unsigned short h {-1};
short int i {0xFFFF};
unsigned short j { 0xDEADBEEF };
Page 105.
U, u: declares that the literal is an unsigned int.
L, l : Literal is a long int.
There is no short qualifier for literals.
Good practice: ensure that initialising literals are qualified to be the same type and signedness as the object they are initializing. Examples:
unsigned int f {100U}; // unsigned int
unsigned long g { 0xABCD1234UL }; // unsigned long
- What are enumerated types in C++? What’s the difference compared to C?
- How enums have changed since C++11?
- What was the most confusing part for enums in C? Hos did that change since C++11?
Page 109-110-111
What is a constexpr function and when can it be used?
Page 114.