Wednesday, March 19, 2008

The const phenomenon

These are some basic and quite interesting things about const where a programmer can does mistakes. Although I have tried to make all the concepts correct , but if anybody find any incorrect information in it , then please let me know.

1.
most fundamental things about const :
a) const fred* const ptr : neither ptr nor *ptr can changed
b) fred * const ptr : ptr can't be changed but *ptr can't be changed
c) const fred * ptr : ptr can change but *ptr can't be changed
d)const fred & kk : const fred& is a reference to a fred that can't be changed via that reference ( here kk )
for ex:
calss fred { public: int i; fred(){i=0;} }
int main()
{
fred kk1,kk2;
const fred & kk=kk1;
kk1.i=5;
//kk.i=10;
return 0;
}
here kk.i will prompt for an error.
fred & const : doesn't make sense, because we can never rebind the reference so that it refers to a different object.

2.
Const Correctness :
If we have to avoid making unexpected changes to objects , then it can be done by proper using of keyword const. The c++ compiler detects many unexpected changes to objects and it flags these violations with error messages at compile time.
for ex:
void f( const string& s)
throw(); // parameter is received by reference to const

3.
Const correctness : It should be done sooner rather than later ( to avoid the ripple effect in function calling, where one function contain oth rs )

4.
Inspector & Mutator class:
fred {
public:
int kk() ; // mutator
int kk1() const ; // inspector
}

An inspector is a member function that returns information about an object's state without changing the object's abstract state. A mutator changes the object's abstract state.Whenever the member function wants to guarntee that it won't make any observable changes to its "this" object , then we make this member function constant.

5.
Const, formal parameter and pass by value :
We should not use const for formal parameter types, that are passed by value, because a const on a pass by value parameters affects only the code inside the function, it doesn't affect the caller. For ex,
replace f(const fred x) with either f(const fred &x ) or f(fred x)
replace f(fred * const p) with f(fred * p).

6.
If we want to change the data member inside const member function, the we have to make that data member mutable or if it can't be done then we should use const_cast.

Tuesday, March 18, 2008

FACTS ABOUT STATIC VARIABLES IN C++

1.
Most fundamental:
Static data member and static member function:
Static data members are often referred to as class data, and static member functions are often referred to as class services or class methods. Static data members are like data located in the factory rather than in the objects produced by the factory. Just as a factory exists before it produces its first object, class (static) data can be accessed before the first object is instantiated as well as after the last object has been destroyed. Static data member is like a global variable with a funny name that doesn't need to be public. Static member functions and top level friend functions are similar in that neither has an implicit this parameter, and both have direct access to the class's private: and protected: parts.
Syntax for accessing static data member and member function:
int fred:: x_=3;
classname::staticmember()
If we want to acess static member function classname::f() from another member function of class classname then there is a exception.
For example,
when classname::f() is a protected: static memeber function of the class ,
simply write f() rather than classname::f().


.2.
Inline fuctions and static data members:
It is not safe to use static data member through inline functions. The static data member might not be initialize before using it in inline fuction. So, it can create a problem in code. Try to avoid accessing static data member through inline function. Some, but not all compilers initialize static data member before the main begin. Therefore the success or failure of the code might even depend on the order that object files are passed to the linker, and some visual environment hide the linker so well, that many programmers don't even know the order in which object files are passed to the linker.




3.
Class with static data member and linker error:
For the safe hand, Static data member must be explicitly defined in exactly one source file. Ignorance of the static initialization order problem can result in application crashes.




4.
const static data member and its initialization:
A const data member is described in the class and is normally defined ( and initialized ) in a source file. But in some cases it can be initialized in the class body proper. For ex: int, unsigned long, char, and so on, are special: they can be initialized where they are declared in the class body proper.

Sample file, fred.hpp
#includeusing namespace std;
class barney { };
class fred {
public : ..........
private:
static const int i_ =42;
static const char c_= 'z';
static const float x_;
static const string s_;
static const barney b_;
}
source file
fred.cpp
#include "fred.hpp"
const float fred::x_=3.14;
const string fred::s_"hello";
const barney fred::b_;




5.
Implementing a function that maintains its state between the class:
To maintain a function that maintains its state between the call, we should not use the function which contain static variable, rather we should use functionaid or we should use functor. Because the static variable create subtle error in case of shared memory, and multi threading environment.