Sunday, June 3, 2007

Item 4

make sure the objects are initialized before they are used:

in general if you are in c part of c++ and initialization wud incur a runtime cost then it is not guaranteed to take place ( actual rules are complex).

always initialize your objects

differentiate between initialization and assignment.

for built in types their is no difference between initialization and assignment

the relative order of initialization of non local static objects defined in different translation units is undefined.

it is so because determining the correct order is unsolvably hard.

you can use static functions to get around this problem. but they are a problem in multithreaded functions.

what does it mean when you say " any kind of non const static object local or non local is trouble waiting to happen in the presence of multiple threads " ?

the easiest way around is to invoke them in proper order in the single threaded startup process.

things to remember:
manually initialise objects of built in type because c++ only sometimes initializes them itself.
in a constructor prefer use of the member initialization list to assignment inside the body of the constructor. list data members in the initialization list in the same order they are declared in the class.
avaoid initialization order problems across translation units be replacing non local static objects with local static objects.

No comments: