3

https://stackoverflow.com/a/8839647/462608

Use the static initializer:

public class MyClass
{
    static {
    //init
    }
}

Can something similar be done in C++?
Actually, I need to initialize some variables, but I don't want to create an object.

Community
  • 1
  • 1
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

2 Answers2

6

If the variables are static members, not only are you able to initialize them, but you must initialize them.

There's no direct equivalent of Java initializer lists, but something similar can be done calling a function to initialize a static member:

class X
{
    static bool x;
}

bool foo()
{
    //initialization code here
}

bool X::x = foo();

This is for cases with intense logic. If all you want is to initialize static members, just do it similar to X::x.

Actually, I need to initialize some variables, but I don't want to create an object.

If the variables are outside the class, initialize them directly (don't need calling code for that).

If the variables are static members of the class, use one of the above approaches.

If the variables are non-static members, they simply don't exist without an object.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • well, thanks for info, but in this particular question, I was taling about normal members. I want the initialization function to be called only once, that's why talked of having a constructor. – Aquarius_Girl Sep 25 '12 at 12:44
  • 2
    @AnishaKaul members exist for each instance of the class, so you want only the first one that's created to be initialised? This makes no sense. – Seth Carnegie Sep 25 '12 at 12:45
  • `so you want only the first one that's created to be initialised?` Where did I say that? @SethCarnegie – Aquarius_Girl Sep 25 '12 at 12:46
  • @SethCarnegie It makes sense if we're talking about singleton pattern... but I wonder if it's the case. For the question itself, the static members of a class doesn't belongs to any instance of the class (they belong to all instances), so doesn't make sense to initialize it on the ctor. They must be initialized *outside* the class as suggested on other answers. – PaperBirdMaster Sep 25 '12 at 12:49
  • @PaperBirdMaster A singleton is an instance. – Luchian Grigore Sep 25 '12 at 12:49
  • @AnishaKaul "normal" members (I assume you mean non-static) don't exist without creating an object. Pure and simple... – Luchian Grigore Sep 25 '12 at 12:50
  • @AnishaKaul from the link you posted (some logic in the initialization of the member), I'd go with the method approach. The method will be called when initializing `X::x`. – Luchian Grigore Sep 25 '12 at 12:53
  • @LuchianGrigore yes, singleton is an instance, and static members does not belongs to any specific instance... I dont know if I'm misunderstanding the OP question I'm just wondering about if the singleton pattern suits the OP requeriments. – PaperBirdMaster Sep 25 '12 at 12:55
  • I realize that I shouldn't have used the word "member". My fault. Let me think on the design more, and then respond. Thanks Luchian. – Aquarius_Girl Sep 25 '12 at 12:55
  • `If the variables are outside the class, initialize them directly (don't need calling code for that)` But, then, wouldn't I have to make them global? (for initializing outside function) – Aquarius_Girl Sep 25 '12 at 12:56
  • @AnishaKaul a static member is a global anyway :) – Luchian Grigore Sep 25 '12 at 12:59
  • Luchain, can a static member be accessed "directly" in any function by anyone? – Aquarius_Girl Sep 25 '12 at 13:00
  • @AnishaKaul if it's public, yes. – Luchian Grigore Sep 25 '12 at 13:01
  • There is a confusion it seems. I am talking about the static member declared inside a class. – Aquarius_Girl Sep 25 '12 at 13:06
  • @AnishaKaul a public static class member can be accessed from anywhere without an object. This is valid for Java as well. – Luchian Grigore Sep 25 '12 at 13:07
1

Static variables of a class are initialized in .cpp file:

MyClass.h

class MyClass {
public:
   static void f1();
   static void f2();
private:
   static int a1;
   static int a2;
   static int a3;
};

MyClass.cpp

   int MyClass::a1 = 7;
   int MyClass::a2 = 8;
   int MyClass::a3 = MyClass::a1 + MyClass::a2;

   void MyClass::f1()
   {
      a1 = 7;
   }
   void MyClass::f2()
   {
      cout << a2 << endl;
   }

If you want non trivial initialization of static member variables - group them in inner class with constructor:

MyClass.h

class MyClass {
public:
   static void f1();
   static void f2();
private:
   struct Data {
      Data();
      int a1;
      int a2;
      int a3;
   };
   static Data data;
};

MyClass.cpp

   MyClass::Data MyClass::data;
   MyClass::Data::Data() : a1(0), a2(0), a3(0)
   {
      // it is just an example ;)
      for (int i = 0; i < 7; ++i)
      {
         a1 += 2;
         s2 += 3;
         a3 += a1 + a2;
      }
   }

   void MyClass::f1()
   {
      data.a1 = 7;
   }
   void MyClass::f2()
   {
      cout << data.a2 << endl;
   }
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112