Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the rationale for not having static constructor in C++?

What is the rationale for not having static constructor in C++?

If it were allowed, we would be initializing all the static members in it, at one place in a very organized way, as:

//illegal C++ class sample { public:      static int some_integer;     static std::vector<std::string> strings;      //illegal constructor!     static sample()     {        some_integer = 100;        strings.push_back("stack");        strings.push_back("overflow");     } }; 

In the absense of static constructor, it's very difficult to have static vector, and populate it with values, as shown above. static constructor elegantly solves this problem. We could initialize static members in a very organized way.

So why doesn't' C++ have static constructor? After all, other languages (for example, C#) has static constructor!

like image 932
Nawaz Avatar asked Mar 14 '11 16:03

Nawaz


People also ask

Why static is not used in constructor?

The constructors in Java can not be static because if the constructors are marked as static, they can not be called from the child class; thus, the child class's object will not be created. The program will not be compiled and throw a compile-time error.

Why do we need static constructor?

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed only once. It is called automatically before the first instance is created or any static members are referenced.

Why a static constructor can not be a parameterized constructor?

A static constructor is called automatically to initialize the class before the first instance is created, so we can't send it any parameters. You cant pass parameters to Static Constructors, because you cannot access any non-static member outside of a static method (constructor too).

What happens if we declare constructor as static?

No, we cannot define a static constructor in Java, If we are trying to define a constructor with the static keyword a compile-time error will occur. In general, static means class level. A constructor will be used to assign initial values for the instance variables.


2 Answers

Using the static initialization order problem as an excuse to not introducing this feature to the language is and always has been a matter of status quo - it wasn't introduced because it wasn't introduced and people keep thinking that initialization order was a reason not to introduce it, even if the order problem has a simple and very straightforward solution.

Initialization order, if people would have really wanted to tackle the problem, they would have had a very simple and straightforward solution:

//called before main()  int static_main() {  ClassFoo(); ClassBar();  } 

with appropriate declarations:

class ClassFoo {  static int y;   ClassFoo() {    y = 1;   } }  class ClassBar {   static int x;   ClassBar() {    x = ClassFoo::y+1;   } } 

So the answer is, there is no reason it isn't there, at least not a technical one.

like image 125
lurscher Avatar answered Sep 24 '22 02:09

lurscher


This doesn't really make sense for c++ - classes are not first class objects (like in e.g. java).

A (static|anything) constructor implies something is constructed - and c++ classes aren't constructed, they just are.

You can easily achieve the same effect though:

//.h struct Foo {   static std::vector<std::string> strings; }; //.cpp std::vector<std::string> Foo::strings(createStrings()); 

IMO there's just no need for one more syntactic way of doing this.

like image 30
Erik Avatar answered Sep 21 '22 02:09

Erik