0% found this document useful (0 votes)
43 views5 pages

C TipsAndTricks

The document discusses two topics related to C++ tricks and tips: 1. Avoid double initialisation by always initializing variables to prevent undefined behavior and make bugs easier to find. Initialization is better done in the constructor initialization list rather than the constructor body for efficiency. 2. The C++ compiler silently writes and calls default versions of certain functions like the constructor, copy constructor, assignment operator, and destructor unless they are declared by the programmer. These generated functions may not do what is expected.

Uploaded by

fukcscribd
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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views5 pages

C TipsAndTricks

The document discusses two topics related to C++ tricks and tips: 1. Avoid double initialisation by always initializing variables to prevent undefined behavior and make bugs easier to find. Initialization is better done in the constructor initialization list rather than the constructor body for efficiency. 2. The C++ compiler silently writes and calls default versions of certain functions like the constructor, copy constructor, assignment operator, and destructor unless they are declared by the programmer. These generated functions may not do what is expected.

Uploaded by

fukcscribd
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 PDF, TXT or read online on Scribd
You are on page 1/ 5

C++ Tricks and Tips

November 14, 2011

Contents
1 Avoid Double Initialisation 2 Know what functions C++ silently writes and calls 2 4

Chapter 1

Avoid Double Initialisation


Reading an uninitialised value results in undened behaviour. Because of C heritage, things that will incur a runtime cost during initialisation are left uninitialised e.g. an array. However, newer C++ additions (e.g. std::vector) are initialised. There are rules which describe when variables/objects are initialised and when they are not. These rules are too many and too complex to bother remember them so its best to initialise everything to some value even if this particular value doesnt make any sense. If everything is initialised when debugging youre more likely to encounter the same (wrong) result if youve initialised variables whereas if you havent the value will be random each time. This will be a hard bug to track down. For built in types initialisation is obvious: int a = 0; float b = 0.0; For user dened types (i.e. classes dening objects) there are several ways to do this. One way is to put all initialisations in the constructor. For example: Class::Class() { a = 0; b = 0.0; } There is however a better way: put all initialisation in the constructor initialisation list. For example: 2

CHAPTER 1. AVOID DOUBLE INITIALISATION Class::Class () : a(0), b(0.0) {} Class::Class (int defaultA, float defaultB) : a(defaultA), b(defaultB) {}

The reason this is better is because initialisation in C++ means that data members of an object are initialised before the body of the constructor is entered. Inside Classs constructor the data members are not being initialised - they are being assigned. Initialisation took place earlier when their default constructors were automatically called prior to entering Class::Class(*). This means that youve allowed the compiler to initialise them to some value without making use of the option available to you to tell the compiler what this initialisation value should be. Initialising variables in the constructor initialisation list means that you can call the default constructor or an overloaded copy-constructor and pass it an initialisation value. This is more ecient than allowing the compiler to call the default constructors and then making extra assignment calls in the constructor body. However, I should stress that because a, b are built in types there is no guarantee that their constructors were called. Again the compiler has the option not to assign default values if this will create extra calls (as mentioned earlier). For other types such as types from the Standard Library (such as std::vector or std::list) initialisation will take place before Classs constructor is entered. There is a gotcha however. Within a class data members are initialised in the order in which they are declared, not in the order you put them in the initialisation list. It is perfectly legal to list them in any order you wish in the constructor initialisation list. It will serve you well, however, to make sure variables declaration order and their order in the initialisation list is the same.

Chapter 2

Know what functions C++ silently writes and calls


Compilers in C++ will declare certain function unless you declare them yourself. In other words compilers will generate a default version. They will do that only if the functionality provided by these functions is required (used) somewhere in the program. These functions are: a default constructor, a copy constructor, an assignment operator, a destructor. This means that this: class Class{}; is eectively equal to this: class Class { Empty() { ... } // default constructor Empty(const Empty& rhs) { ... } // copy constructor Empty& operator=(const Empty& rhs) { ... } // copy assignment operator ~Empty() { ... } // destructor };

You might also like