0% found this document useful (0 votes)
145 views25 pages

Constructor PPT Updated

The document discusses effective approaches for writing comprehensible and maintainable C++ code. It recommends: knowing functions silently written by C++; preferring initialization to assignment in constructors; listing members in initialization order; preventing implicit conversions; declaring copy constructors and assignment operators for classes with dynamic memory; making base class destructors virtual; and avoiding virtual function calls during construction and destruction. The goal is to write C++ code that is more portable, extensible, efficient, and behaves as expected.

Uploaded by

Sourabh Agrawal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views25 pages

Constructor PPT Updated

The document discusses effective approaches for writing comprehensible and maintainable C++ code. It recommends: knowing functions silently written by C++; preferring initialization to assignment in constructors; listing members in initialization order; preventing implicit conversions; declaring copy constructors and assignment operators for classes with dynamic memory; making base class destructors virtual; and avoiding virtual function calls during construction and destruction. The goal is to write C++ code that is more portable, extensible, efficient, and behaves as expected.

Uploaded by

Sourabh Agrawal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Effective CPP

Sourabh Agrawal Click to edit Master subtitle style 11-Aug-2011

5/26/12

Sourabh Agrawal

AGENDA

To suggest a approach to effective way of writing C++ which are more comprehensible, maintainable, portable, extensible, efficient, and likely to behave as you expect .

5/26/12

Sourabh Agrawal

Topics

Know what functions C++ silently writes and calls

Prefer initialization to assignment in constructors

List members in an initialization list in the order in which they are declared

Prevents Constructor from being used to perform implicit type conversions

Declare a copy constructor and an assignment operator for classes with dynamically allocated memory

Declare destructors virtual in polymorphic base classes

5/26/12

Sourabh Agrawal

Functions C++ silently writes and calls in a Class


Click to edit Master subtitle style

5/26/12

Sourabh Agrawal

class Empty { }; it's essentially the same as if you'd written this: class Empty { public: Empty() { }
5/26/12 ~Empty()

{}

Sourabh Agrawal

Prefer initialization to assignment in constructors


Click to edit Master subtitle style

5/26/12

Sourabh Agrawal

Class A { public: A (string initName ) { name = initName; } private: string name; };


5/26/12 Sourabh Agrawal

Class A { public: A (string initName ): name (initName) { } private: string name; };


5/26/12 Sourabh Agrawal

Const and Reference class member variables must be initialized in constructor.

5/26/12

Sourabh Agrawal

List members in the class in its dependency order


Click to edit Master subtitle style

5/26/12

Sourabh Agrawal

template<class T> class Array { public: Array(int low, int high); //Constructor Declaration size_t size; elements in array vector<T> data; array data is stored int lBound, hBound; bound, higher bound Agrawal 5/26/12 Sourabh // No. of // the // lower

Prevents Constructor from being used to perform implicit type conversions


Click to edit Master subtitle style

5/26/12

Sourabh Agrawal

class ABC { public: ABC(int x) { } }; void function(ABC Object) {


5/26/12

cout<<Function Called<<endl; Sourabh Agrawal

class ABC { public: explicit ABC(int x) // Not allowing implicit casting of integer to ABC { } }; void function(ABC Object) {
5/26/12 Sourabh Agrawal

Declare a copy constructor and an assignment operator for classes with dynamically allocated memory

5/26/12

Sourabh Agrawal

class String { char *data; public: String::String(const char *value) { if (value) { data = new char[strlen(value) + 1]; strcpy(data, value); } } String::~String() { delete [] data; } };

Bitwise Copying Bitwise Copying Sourabh Agrawal

int main()

5/26/12

String a("Hello"); String b("World");

b = a; // Both b and a data* are pointing to same memory location Memory Leak

delete a 5/26/12 Leads to Memory Sourabh Agrawal Corruption

Declare destructors virtual in polymorphic base classes


Click to edit Master subtitle style

5/26/12

Sourabh Agrawal

Acc. to C++ Standard:

When you try to delete a derived class object through a base class pointer and the base class has a nonvirtual the results are undefined.

Polymorphic base classes should declare virtual destructors. If a class has any virtual functions, it should 5/26/12 Sourabh Agrawal have a virtual destructor.

Handle assignment to self in operator=


Click to edit Master subtitle style

5/26/12

Sourabh Agrawal

If you can detect an assignment to self at the top of your assignment operator(s), you can return right away, possibly saving a lot of work that you'd otherwise have to go through to implement assignment A more important reason for checking for assignment to self is to 5/26/12 Sourabh Agrawal ensure correctness.

Have operator= return a reference to *this

5/26/12

Sourabh Agrawal

Prevent exceptions from leaving destructors

Destructors should never emit exceptions. If functions called in a destructor may throw, the destructor should catch any exceptions, then swallow them or terminate the program. If class clients need to be able to react to exceptions thrown during an 5/26/12 Sourabh Agrawal operation, the class should provide a

Never call virtual functions during construction or destruction


Don't call virtual functions during construction or destruction, because such calls will never go to a more derived class than that of the currently executing constructor or destructor.

5/26/12

Sourabh Agrawal

Thank You !!!


References: Click to edit Master subtitle style Effective C++ C++ Standard Draft

5/26/12

Sourabh Agrawal

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy