Effective C++ Flashcards
What is “slicing” ?
Passing object of derived class to a function taking base class, by value. All derived class specializations are “sliced off”.
What are the prefered alternatives for constants instead of #define macros?
const objects and enums
What is prefered instead of function-like macros?
Inline functions
What is bitwise (physical) constness?
Type of constness that ensures that object’s data members are not modified .
What is logical constness?
Const member function might modify some of data member’s bits but only in ways that clients cannot detect.
How should data members be enlisted in the initialization list?
In same order they appear in the class declaration.
What is a better alternative to non-local static objects?
Local static objects (function scope …)
What is preferable for initializing objects in constructor instead of assignment?
Initialization within initialization list.
What is a translation unit?
Source code giving rise to a single object file.
How are data members initialized within a class?
In order they appear.
How to disallow automatic generation of copy/move constructors and assignment operators ?
By declaring them private or deleting them (C++11).
Which type of classes should / must have a virtual destructor?
Polymorphic base classes.
How should destructor behave in terms of exception handling?
It should catch any exceptions, “swallow” them or terminate the program.
Why should virtual functions never be called from the constructor?
Because such calls will never go to a more derived class than that whose scope is currently executed.
What should be the return type of assignment operators?
A reference to this.
What is a good strategy for preventing resource leaks in terms of resource management?
RAII
Why should newed objects stored in smart pointers be standalone statements?
To prevent resource leaks in cases when exceptions are thrown.
What are the two characteristics one should strive to with interface definition?
Good interfaces are easy to use correctly and hard to use incorrectly.
What are some of the ways to prevent errors in interface/data usage?
Define new types, provide a restricted set of operations on types, constrain object values, eliminate client resource management.
Pass by const-ref instead of pass-by-value … why?
It is more efficient if “heavy” objects are passed around and it avoids the slicing problem.
What are some of the exemptions that should not be passed as (const) references?
STL iterators, function objects, built-in types.