Effective C++ Flashcards

1
Q

What is “slicing” ?

A

Passing object of derived class to a function taking base class, by value. All derived class specializations are “sliced off”.

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

What are the prefered alternatives for constants instead of #define macros?

A

const objects and enums

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

What is prefered instead of function-like macros?

A

Inline functions

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

What is bitwise (physical) constness?

A

Type of constness that ensures that object’s data members are not modified .

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

What is logical constness?

A

Const member function might modify some of data member’s bits but only in ways that clients cannot detect.

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

How should data members be enlisted in the initialization list?

A

In same order they appear in the class declaration.

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

What is a better alternative to non-local static objects?

A

Local static objects (function scope …)

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

What is preferable for initializing objects in constructor instead of assignment?

A

Initialization within initialization list.

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

What is a translation unit?

A

Source code giving rise to a single object file.

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

How are data members initialized within a class?

A

In order they appear.

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

How to disallow automatic generation of copy/move constructors and assignment operators ?

A

By declaring them private or deleting them (C++11).

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

Which type of classes should / must have a virtual destructor?

A

Polymorphic base classes.

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

How should destructor behave in terms of exception handling?

A

It should catch any exceptions, “swallow” them or terminate the program.

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

Why should virtual functions never be called from the constructor?

A

Because such calls will never go to a more derived class than that whose scope is currently executed.

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

What should be the return type of assignment operators?

A

A reference to this.

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

What is a good strategy for preventing resource leaks in terms of resource management?

A

RAII

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

Why should newed objects stored in smart pointers be standalone statements?

A

To prevent resource leaks in cases when exceptions are thrown.

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

What are the two characteristics one should strive to with interface definition?

A

Good interfaces are easy to use correctly and hard to use incorrectly.

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

What are some of the ways to prevent errors in interface/data usage?

A

Define new types, provide a restricted set of operations on types, constrain object values, eliminate client resource management.

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

Pass by const-ref instead of pass-by-value … why?

A

It is more efficient if “heavy” objects are passed around and it avoids the slicing problem.

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

What are some of the exemptions that should not be passed as (const) references?

A

STL iterators, function objects, built-in types.

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

Why should data members be declared private?

A

Because it gives clients syntactically uniform access to data, restricts access control, allows invariants to be enforced and offers class author the flexibility to change the logic behind the scenes.

23
Q

What is a Pimpl idiom?

A

Pointer to implementation “design pattern”.

24
Q

What is a good practice to avoid writing casts in code?

A

Put the casting into a function that should be called instead of repetitive casts -> like std::move()

25
Q

What is a basic guarantee in terms of exception handling?

A

Everything in the program will stay in a valid state; No objects or data structures will become corrupted.

26
Q

What is a strong guarantee in terms of exception handling?

A

State of the program will remain unchanged. Strong guarantee performs in transaction style. Either all operations succeed or none are applied in case of failure.

27
Q

What types of functions should be inlined?

A

Small and frequently called functions.

28
Q

What is a “cost” of inlining?

A

It increases object code size.

29
Q

What is a 80-20 rule?

A

Program spends 80% of its time executing only 20% of its code -> do the optimizations in this 20% of code.

30
Q

What is a single most important rule of OOP in CPP?

A

Public inheritance means “is-a”.

31
Q

What is an “is-a” relationship?

A

Everything that applies to base classes must also apply to derived classes, because every object of derived class is also an object of base class.

32
Q

How can “hiding” of inherited names be solved in derived classes?

A

By employing using declarations.

33
Q

What is the purpose of declaring a pure virtual function in terms of inheritance?

A

To inherit an interface in derived class.

34
Q

What is the purpose of declaring a simple virtual function, in terms of inheritance?

A

To inherit an interface and a default implementation in a derived class.

35
Q

What is the purpose of declaring a non-virtual function, in terms of inheritance?

A

To inherit an interface and a mandatory implementation.

36
Q

What are the two alternatives to virtual functions?

A

NVI (Non-virtual interface) idiom and Strategy pattern.

37
Q

What is NVI?

A

Non-Virtual Interface is an approach in definition of classes interface when public methods are not virtual but they call private virtual functions.

38
Q

Can private virtual functions be redefined?

A

Yes.

39
Q

What are 3 approaches to utilize Strategy Pattern instead of virtual functions?

A
  1. Replace virtual functions with function pointer data members.
  2. Replace virtual functions with std::function data members.
  3. Replace virtual functions in one hierarchy with virtual functions of another hierarchy -> implement some kind of composition.
40
Q

Why must inherited non-virtual functions never be redefined?

A

Because it breaks the is-a principle of public inheritance.

41
Q

Why must inherited default parameters of the function never be redefined?

A

Because default parameters are statically bound and will never be dynamically applied if modified. If derived class is referenced by base class.

42
Q

What is an application domain?

A

Objects that are modeled based on the real world.

43
Q

What is an implementation domain?

A

Objects that represent core implementation details like buffers, mutexes etc.

44
Q

When does private inheritance makes sense?

A

When we want to inherit an implementation and not the interface and when a derived class needs access to protected base class members (where composition does not allow) or needs to redefine inherited virtual functions.

45
Q

What is the problem of putting non-type template parameters in template definition?

A

They increase code bloat.

46
Q

What is usually a good practice to avoid non-type template parameters?

A

To use them as function parameters or class data members.

47
Q

What are traits classes?

A

Classes that provide type information during compilation.

48
Q

What does “Turing-complete” mean?

A

To be powerful enough to compute anything.

49
Q

What is “set_new_handler”?

A

Function that can be used to set a callback function for operator new which is invoked when memory allocation request can not be fulfilled.

50
Q

When does overloading new and delete make sense?

A

When we want to improve performance, put debugging info into memory allocation.

51
Q

How should zero-byte requests be handled within operator new?

A

They should allocate space for one member and return a valid pointer to it.

52
Q

What procedures should be performed when writing operator new?

A

It should run in an infinite while loop trying to allocate memory and calling new_handler callback on each failure to do so and it should take care of zero-bytes allocation requests.

53
Q

What is a placement new operator?

A

Placement new operator is a new operator taking extra arguments other than mandatory size_t size.