Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: non-const static data member must be initialized out of line

Tags:

c++

class

static

class Solution {
    public:

     static int m=INT_MIN; // it shows error: non-const static data member must 
                               be initialized out of line.(why?)
                                  using "int m=INT_MIN" is fine. 
      int func(TreeNode*root){
        if(root==NULL){
            return 0;
        }

        int l=max(func(root->left),0);
        int r=max(func(root->right),0);

        m=max(l+r+root->val,m);

        return max(l,r)+root->val;

    }


    int maxPathSum(TreeNode* root) {

        if(root==NULL)
        {
         return 0;
        }
        m=INT_MIN;
        int x=func(root);
        return m;

    }
};

I need to update the value of variable m. Therefore I am using static int data type. But the following error is coming. Using int instead of static int is working fine. But why is static int giving error?

compilation error

like image 614
Jaswant Arya Avatar asked Apr 30 '20 07:04

Jaswant Arya


People also ask

How do you initialize a static member in C++?

We can put static members (Functions or Variables) in C++ classes. For the static variables, we have to initialize them after defining the class. To initialize we have to use the class name then scope resolution operator (::), then the variable name. Now we can assign some value.

What is non static data member?

Non-static data members are the variables that are declared in a member specification of a class.

What is static member function in C++?

The static member functions are special functions used to access the static data members or other static member functions. A member function is defined using the static keyword. A static member function shares the single copy of the member function to any number of the class' objects.

Why can't I initialize static data members in a class?

Great question and makred answer. Link that helped me: msdn.microsoft.com/en-us/library/0e5kx78b.aspx Why I can't initialize static data members in class? The C++ standard allows only static constant integral or enumeration types to be initialized inside the class. This is the reason a is allowed to be initialized while others are not.

Why is the initialization of a static function allowed inline?

The static member is also constant, then its initialization is allowed inline because the compiler can make its own optimisations and treat this member as a compile-time constant because it is guaranteed that its value will never change.

Is it possible to use static const instead of inline?

This is not a problem with inline instead). static const A b = A (); is not allowed and must be constexpr or inline in order to permit an initialiser for a static object with class type i.e to make a static member of class type more than a declaration.

What happens if a static int is non-out of line?

If you performed static int a = 3; in a class in a header included in all a translation units then you'd get multiple definitions. Therefore, non out-of-line definition of a static is forcibly made a compiler error.


Video Answer


1 Answers

Bjarne Stroustrup explains this here:

A class is typically declared in a header file and a header file is typically included into many translation units. However, to avoid complicated linker rules, C++ requires that every object has a unique definition. That rule would be broken if C++ allowed in-class definition of entities that needed to be stored in memory as objects.

As said by Stroustrup, every class needs a unique definition. Now, as we know static members are associated directly with their class. Now consider the two cases:

  1. The static member is also constant, then its initialization is allowed inline because the compiler can make its own optimisations and treat this member as a compile-time constant because it is guaranteed that its value will never change. So, as the value of this member is fixed, the definition of the class with which this member is associated is also fixed. So, the initialization is allowed inline.

  2. The static member is not constant. Then its value can change later on during the execution of the program. So, the compiler can not make compile-time optimisations on this member. Hence, to prevent the complications that may arise while trying to initialize such a member when the class is loaded, inline initialisation of such members is not allowed.

PS: When I heard about this concept the very first time, I was also confused because it is not in accordance with the principle of orthogonality that is a feature desired by programmers. The principle of orthogonality will state that since we can combine int and static; and int and const, we should be able to write static const int and static int in a similar fashion. But this case here is an example of a situation where the developer of a language has to give up orthogonality for the users of the language in exchange of the simplicity of the compilation process.

Have a look at the concept of orthogonality here

like image 81
Devansh Maurya Avatar answered Oct 04 '22 01:10

Devansh Maurya