C++ Q &A

(1) Linker error ;static variable initialized inside a constructor!!!!!!!!

Never initialize static members of a class inside a class constructor,we can initialize these variables explicitly in main.cpp file.

ex:
//main.hh
class dummy
{Private:
static int dummy_var;
};

//Main.cpp
#include "main.hh"
int dummy::dummy_var = some value;


(2)
Type casting in C++

New operators
static_cast,dynamic_cast,reinterpret_cast and const_cast are available on some of the compilers.
And its better
we should familiarize ourselves with alternative cast techniques to produce better code to avoid confuse with new type casts.

C type still have benefits over C++ type casting.
It's less verbose.
It frees you from the confusion between static_cast and reinterpret_cast.
It can perform two cast operations in a single expression.
Why I Hate C++ Cast Operators by Danny Kalev
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=285

If one is dare to learn and get exact purpose of new casting operators in C++.its good to read following article at by Scott Mayers,

Prefer C++-style casts by Scott Mayers
http://www.ishiboo.com/~nirva/c++/eff/MEC/MI2_FR.HTM


Article:
Casting in C++: Bringing Safety and Smartness to Your Programs

http://www.acm.org/crossroads/xrds3-1/ovp3-1.html


(3) Automatically Indenting C++ programms


Some available commands are listed below.
cb
is a program which does simple indenting, but is not very tailorable. It also indents with tabs, so you must post process with a program like expand to replace tabs with the appropriate number of spaces.
indent
is a very flexible indenting tool, with lots of options about how indenting is performed.
     indent -bad -bap -nbc -br -nce -i4
or
indent -bad -bap -nbc -bl -nce -i4
are commands which approximate our recommended FPCL coding standards.
example:
>/ indent oldfile newfile
-bad -bap -nbc -br -nce -i4

cb and indent come with the SUNWSpro compiler suite from Sun.


(4)
Difference between static global variable and static local variable?

Default initial value is 0 for these two variables, and

Static global variable will be accessed in the file it exists, so that we can use any where in functions/modules in same file itslef.But we cannot use it in other files, even after mentioning it as extern.

In static local variable, the life is extended (i.e. active in memory) between function calls, but the scope is limited to the function in which it is defined. Thus a static local variable acts as a semi-global variable.


No comments :

Post a Comment