The Knowledge - Modern C++ Flashcards

1
Q

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;
    }
}
A

See page 16 of the manual. In general, the inner declaration overrules the outer declaration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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?

A

The scope resolution operator can be used:

std::cout &laquo_space;::x &laquo_space;std::endl;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the three object lifetimes? Describe the lifetime of each one of them.

A
  1. Automatic objects: they exist from the point of defintion to the end of the closing block (function).
  2. 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.
  3. Dynamic objects: from the “new” until the “delete” (the programmer needs to manually delete these objects!!)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the rules for the “internal linkage” of static global objects? Provide an example.

A

Applying the static keyword to an object defined outside of any block gives the object an internal linkage. (page 22).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the rules for STATIC AUTOMATIC obejcts?

What’s the advantage of using a static automatic object?

A

(page 23 manual)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does the memory allocation of a program look like? Describe the different sections.

A

(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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does the memory allocation of a program look like? Describe the different sections.
What about data definitions?

A

(page 25-26 of the manual)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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?

A

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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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?

A
int main() {
    Example ex4 { .i = 100,   .c = 'x'  };
}
=========================
 Example ex2{};
=========================
Example ex3 {100, 'x'};
==========================
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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
}
A

Brace elisiion is a syntactic mechanism to simplify (in some cases) nested structures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What’s the difference between

Position p1{};

And

Position p1;

?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is good practice for class members initialization?

A

Page 55: should you forget the brace initializer on the class, the defaults will still be applied.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Predict the output of the following code snippet:

==========================================
class Position {
public:
Position ( double azi = 45.0, double elev = 45.0 );
void printRho() { std::cout &laquo_space;rho &laquo_space;std::endl; }
void printTheta() { std::cout &laquo_space;theta &laquo_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?

A
  1. 0
  2. 0

Best coding practice: Always ensure that constructor parameter defaults are the same as the default initializers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are aggragate types in C++?

A

Inherited from C:

Arrays
Structures
Unions

17
Q

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?

A

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.

18
Q
How can we disable pass-by-value semantics for a specific class in modern C++?
What about the assignment operator?
A

Page 84-85. We can declare the copy constructor as “delete”.

19
Q

What’s the “Rule of the Big Three”?

When do you have to provide your own implementation of the 3s?

A

Page 86. Your own implementation is required if your class has to manage the lifetime of another object.

20
Q

What are const functions and what they are needed for?

A

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.

21
Q

What’s the main issue with returning objects by value?

What’s instead a good practice to apply? Provide an example.

A

Page 89.

Good practice: Return Value Optimization

22
Q

What is copy elision and when it happens?

23
Q

What is the best coding practice related to automatic type declaration? Why?

A

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).

24
Q

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?

A

C++14: Avoid auto return-type deduction in non-template code, as it gives no information to client as to the object being returned.

25
Q
  1. Provide an example about how to use std::tuple.
  2. How to cout the values from it?
  3. When using a std::tuple object would be useful?
  4. 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?
26
Q

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.

27
Q

What are literals? How can we declare a sized or signed interger literal?

A

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.

28
Q

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 };

A

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

29
Q
  1. What are enumerated types in C++? What’s the difference compared to C?
  2. How enums have changed since C++11?
  3. What was the most confusing part for enums in C? Hos did that change since C++11?
A

Page 109-110-111

30
Q

What is a constexpr function and when can it be used?