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?
3 Answers
To answer OPs question why
class Solution { public: int m = INT_MIN;
};is fine but
class Solution { public: static int m = INT_MIN;
};is not:
In short: Prefixing a data-member with static fundamentally change its meaning.
Without static, the member variable is part of the class and each instance will provide a separate storage for this member variable.
With static, the member variable has only scope of the class but there will be only one global storage.
Respectively, the initialization has different meanings too.
For non-static member variables, it provides a default initialization which constructors may use (or override).
Demonstration by Example:
#include <iostream>
enum ArgCase1 { Case1 };
enum ArgCase2 { Case2 };
class Solution { public: int m = 123; Solution() = default; // will use m(123) implicitly Solution(ArgCase1) { } // will use m(123) implicitly Solution(ArgCase2): m(456) { } // default of m ignored
};
#define DEBUG(...) std::cout << #__VA_ARGS__ << ";\n"; __VA_ARGS__
int main()
{ DEBUG(Solution sol); std::cout << "sol.m: " << sol.m << '\n'; DEBUG(Solution sol1(Case1)); std::cout << "sol1.m: " << sol1.m << '\n'; DEBUG(Solution sol2(Case2)); std::cout << "sol2.m: " << sol2.m << '\n';
}Output:
Solution sol;
sol.m: 123
Solution sol1(Case1);
sol1.m: 123
Solution sol2(Case2);
sol2.m: 456For static member variables, the initialization would be dangerous. Assuming that a class is declared in a header which is included multiple times, this would result in a violation of the One Definition Rule.
In former times, it was usual to declare the static member variable in the header but to define it in the .cpp-file (which represents the translation unit).
Expample:
solution.h:
#ifndef SOLUTION_H
#define SOLUTION_H
class Solution { public: static int m;
};
#endif // SOLUTION_Hsolution.cc:
// header of this module:
#include "solution.h"
int Solution::m = 123;Since C++17, a new alternative is available – using the keyword inline.
From cppreference.com – static members – Static data members
A static data member may be declared inline. An inline static data member can be defined in the class definition and may specify an initializer. It does not need an out-of-class definition
Example:
solution.h:
#ifndef SOLUTION_H
#define SOLUTION_H
class Solution { public: inline static int m = 123;
};
#endif // SOLUTION_HThe advantage is that there is no .cpp-file needed for this i.e. as it is the class Solution could be provided as header-only source.
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:
The
staticmember is alsoconstant, 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.The
staticmember 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
What you can do is at the place where you are calling the respective functions, first, initialize an int and then pass that int by address into the class methods. Edit the class method definitions accordingly.
int main(){ int m=INT_MIN; Solution *solution = new Solution; solution->func(root,&m);
} 1