September 1, 2011

C++ Static data member variable and its initialization

Question
With regard to static data member initialization, as we are all aware of the syntax used in c++, a sample example is provided below


class Account 
{
    public:
        static double rate() { return interestRate; }
        static void rate(double); // sets a new rate
    private:
        static const int period = 30; // interest posted every 30 days
        static double interestRate;
        double daily_tbl[period]; // ok: period is constant expression
};

// define and initialize static class member
double Account::interestRate = initRate();

int main()
{
    Account ac;
    ...
    ...
}


Now here we see two kinds of initialization of static member variable.

a. The const data member variable is initialized at the time of declaration.
b. The non-const data member variable is initialized outside the class declaration.

My initial view was - this is strange!!!
Is it still retained in language because of legacy mistake? Or is there really a need to have such kind of differentiation?

Answer
I did ponder over this question and posted the same in a popular Q&A portal
(stackoverflow).You can find the original question with detailed reply by members here.
 

Let me start re-discussion the same question in a more detailed fashion.

One of the first pointer is ODR (Wikipedia One Definition Rule).
a. This states that any translation unit - a template/type/function or an object can have no more than one definition.since a definition provides the instance, it can be only one in a translation unit. However, declarations can be many.

b. The same argument extends for the exceutable. In an executable, there can be only one definition for a variable (after scope resolution) and function definitions.

This answers the second question. 


  • If non-const static member was allowed to be defined in the class itself,then it would have been impossible to include these class declaration in different translation units. 
  • Thus to avoid violation of ODR rule,the non-const static data member must be defined outside the class declaration.


Now let us come to the first part of the question (which can be explained much more easily), a const data member can be defined inside the class member for two reasons - 
  • Even if multiple copies are present in each translation unit, the expression will be evaluated during compile time and does not violate the ODR theme.
  • A const must be defined during the creation of the instance (Since it cannot be changed later), if not for the above rule, there would have been chance to change it on every object instansiation or having different definition in each translation unit.

Take Away's

 
Developers need not remember each language syntax and its subtle changes based on memorizing the scenarios under which error are thrown by compilers.
 

Instead simple interpretation and logical understanding of the constraints that forced to structure the language can be studied and thus understand the syntax by logical interpretation.

Regards,
Tech Unravel,
Supreme Debugging.

No comments:

Post a Comment