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.
Why should data members be declared private?
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.
What is a Pimpl idiom?
Pointer to implementation “design pattern”.
What is a good practice to avoid writing casts in code?
Put the casting into a function that should be called instead of repetitive casts -> like std::move()
What is a basic guarantee in terms of exception handling?
Everything in the program will stay in a valid state; No objects or data structures will become corrupted.
What is a strong guarantee in terms of exception handling?
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.
What types of functions should be inlined?
Small and frequently called functions.
What is a “cost” of inlining?
It increases object code size.
What is a 80-20 rule?
Program spends 80% of its time executing only 20% of its code -> do the optimizations in this 20% of code.
What is a single most important rule of OOP in CPP?
Public inheritance means “is-a”.
What is an “is-a” relationship?
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.
How can “hiding” of inherited names be solved in derived classes?
By employing using declarations.
What is the purpose of declaring a pure virtual function in terms of inheritance?
To inherit an interface in derived class.
What is the purpose of declaring a simple virtual function, in terms of inheritance?
To inherit an interface and a default implementation in a derived class.
What is the purpose of declaring a non-virtual function, in terms of inheritance?
To inherit an interface and a mandatory implementation.
What are the two alternatives to virtual functions?
NVI (Non-virtual interface) idiom and Strategy pattern.
What is NVI?
Non-Virtual Interface is an approach in definition of classes interface when public methods are not virtual but they call private virtual functions.
Can private virtual functions be redefined?
Yes.
What are 3 approaches to utilize Strategy Pattern instead of virtual functions?
- Replace virtual functions with function pointer data members.
- Replace virtual functions with std::function data members.
- Replace virtual functions in one hierarchy with virtual functions of another hierarchy -> implement some kind of composition.
Why must inherited non-virtual functions never be redefined?
Because it breaks the is-a principle of public inheritance.
Why must inherited default parameters of the function never be redefined?
Because default parameters are statically bound and will never be dynamically applied if modified. If derived class is referenced by base class.
What is an application domain?
Objects that are modeled based on the real world.
What is an implementation domain?
Objects that represent core implementation details like buffers, mutexes etc.
When does private inheritance makes sense?
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.
What is the problem of putting non-type template parameters in template definition?
They increase code bloat.
What is usually a good practice to avoid non-type template parameters?
To use them as function parameters or class data members.
What are traits classes?
Classes that provide type information during compilation.
What does “Turing-complete” mean?
To be powerful enough to compute anything.
What is “set_new_handler”?
Function that can be used to set a callback function for operator new which is invoked when memory allocation request can not be fulfilled.
When does overloading new and delete make sense?
When we want to improve performance, put debugging info into memory allocation.
How should zero-byte requests be handled within operator new?
They should allocate space for one member and return a valid pointer to it.
What procedures should be performed when writing operator new?
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.
What is a placement new operator?
Placement new operator is a new operator taking extra arguments other than mandatory size_t size.