effective c++notes
DESCRIPTION
Notes on book 'Effective C++'TRANSCRIPT
![Page 1: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/1.jpg)
U T T A M G A N D H I S Y N E R Z I P
H T T P : / / W W W . L I N K E D I N . C O M / P U B / U T T A M -G A N D H I / 2 1 / 7 A A / 2 4
Effective C++
![Page 2: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/2.jpg)
��
Acknowledgments
� This presentation is based on Scott Meyers well known book ‘Effective C++’
� http://www.amazon.com/Effective-Specific-Improve-Programs-Designs/dp/0321334876
![Page 3: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/3.jpg)
Variable Declaration
� Delay as far as possible ¡ can avoid construction and destruction, ¡ keeps the context in which it is required ¡ for loop construction, consider creating in loop based on cost
of assignment v/s cost of construct destructor
![Page 4: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/4.jpg)
Passing of Parameters
� Pass by value ¡ Primitive types ¡ STL function objects, iterators
� Pass by reference ¡ User defined objects
÷ Efficient than pass by value ÷ No slicing
� Return by reference ¡ Local data should not be returned by reference
![Page 5: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/5.jpg)
const 1/2
� Replacing #define with const ¡ Reduced object code ¡ Better error message ¡ Control over scope
� Enum ¡ Can be used instead of const ¡ Cannot take address of enum
� const_iterator
![Page 6: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/6.jpg)
const 2/2
� const return type � const input parameter � const member function of a class � const overloading of member functions � Use of mutable
![Page 7: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/7.jpg)
casts
� static_cast � dynamic_cast � const_cast � reinterpret_cast
![Page 8: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/8.jpg)
Member initialization list
� Efficiency ¡ Default constructor plus assignment are replaced by copy
constructor � Order of declaration in class
¡ Order of initialization list
� Const and reference member ¡ must be initialized in member initializer list
![Page 9: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/9.jpg)
inlining
� inlining does in-place replacement of code � Improves efficiency � Can lead to increased or decreased code size � Inlining is a request to compiler � Certain functions may not be inlined
¡ Called through function pointers ¡ Containing loops ¡ Virtual functions ¡ Constructor and destructor are worst candidates for inlining
![Page 10: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/10.jpg)
Compilation
� forward declare where possible � Keep declaration and definition in separate files
¡ Not possible for templates
![Page 11: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/11.jpg)
Non-local static object initialization
� The order of initialization in different translation units is undefined ¡ Solution-> use function which return static object by reference
![Page 12: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/12.jpg)
Default functions 1/2
� Default functions ¡ default constructor, ¡ Assignment ¡ copy constructor ¡ destructor ¡ & operator
� If parameterized constructor is user defined, default constructor will not be provided
� default copy constructor does bitwise copy � copy constructor and assignment operator not generated if, there is a ref member or const member
![Page 13: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/13.jpg)
Default functions 2/2
� To avoid default copy constructor ¡ declare them private and don't define them ¡ Derive from base class with private copy constructor ¡ Use boost::noncopyable
![Page 14: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/14.jpg)
constructor destructor
� Virtual destructor ¡ Needed for base classes ¡ Compiler will invoke the base destructor only without it
� Pure virtual destructor ¡ To create abstract classes ¡ Having body in pure virtual destructor will help compiler while
invoking the base class destructor
� Throwing exception ¡ Destructor should never throw exception
![Page 15: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/15.jpg)
Inheritance 1/4
� Design ¡ public inheritance
÷ Should always represent is-a relationship ¢ Penguin is not a bird ¢ Square is not a rectangle
![Page 16: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/16.jpg)
Inheritance 2/4
� Design ¡ Inheritance of interface v/s inheritance of implementation. ¡ public inheritance = inheritance of interfaces ¡ Pure virtual functions = inheritance of interface ¡ impure virtual functions = inheritance of interface plus
inheritance of a default implementation. ¡ Non-virtual functions = inheritance of interface plus
inheritance of a mandatory implementation.
![Page 17: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/17.jpg)
Inheritance 3/4
� Private inheritance (is implemented in terms of ) ¡ When only implementation is required ¡ e.g. class adapter pattern
� Names in derived classes hide names in base classes � To make them visible
¡ Using declaration ¡ Forwarding function
� don't redefine non-virtual inherited function
![Page 18: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/18.jpg)
Inheritance 4/4
� Multiple inheritance complexities ¡ lead to ambiguity issues ¡ May need virtual inheritance. ¡ Virtual inheritance costs
÷ size, speed, and complexity of initialization and assignment. ¡ It's practical when base classes have no data. ¡ Some practical uses
÷ public inheritance for an Interface and private inheritance for implementation
![Page 19: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/19.jpg)
Composition
� has-a relationship ¡ e.g. car has wheels ¡ Aggregation v/s composition
÷ In C++ a class having pointer members is composition whereas class having concrete members is aggregation
÷ Aggregation has full control of aggregated object ÷ Composition can share the contained object with other classes
![Page 20: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/20.jpg)
Virtual Function
� Don’t call virtual function from constructor or destructor ¡ Derived part is not yet constructed when in base class
constructor � Never redefine an inherited default parameter value
¡ default parameter values are statically bound, ¡ virtual functions are dynamically bound.
![Page 21: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/21.jpg)
Operator overloading
� Assignment operator should return reference to *this ¡ To support a = b = c ( chaining )
� Check for self-copy in assignment operator ¡ If copying is deleting the existing object first, the object may
delete itself
![Page 22: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/22.jpg)
Copy constructor
� copy constructor should call base copy constructor
![Page 23: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/23.jpg)
RAII
� Use constructor and destructor for resource management
� Use std::shared_ptr or std::scoped_ptr instead of raw pointers
� Use std::vector instead of arrays � std::shared_ptr, with function object as argument
for action other than delete
![Page 24: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/24.jpg)
Misc
� new[] delete[] should be matched � Namespace usage
¡ package extensibility ¡ better encapsulation ¡ precise control over what to include
![Page 25: Effective c++notes](https://reader034.vdocuments.mx/reader034/viewer/2022051323/5486967c5806b59a588b49ae/html5/thumbnails/25.jpg)
Designing Interfaces
� Simple to use correctly and hard to misuse ¡ Consistency with built-in types
÷ a = b = c ¡ Restriction on types ¡ Constraining object values
÷ Date, Calendar ¡ Eliminating client side resource management