You are on page 1of 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