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.
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.
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
#include
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.

No comments:
Post a Comment