-1

I was looking for a method to initialize a static float inside a structure BUT using the constructor of the struct. In this site there are already solution to initialize the value but I was unable to find a solution that explicitly use the constructor.

The idea is the following:

struct test {
  static const float a;
  int b;
  test(int bb, float a);
};

test::test(int bb, float aa) {
  b=bb;
  a=aa;
}

int main() {
  int bval=2;
  float aval=0.25;
  struct test aaa(bval, aval);
  return 0;
}

How to implement it correctly? Thank you for any advice.

bitmask
  • 32,434
  • 14
  • 99
  • 159
Fabrizio
  • 927
  • 9
  • 20
  • 1
    **Why**? `static` data belongs to the class; the constructor is run on instances. Why would you want to keep overwriting the value that's shared between all instances, each time a new one is created? What are you hoping to accomplish? – Karl Knechtel Jan 26 '12 at 23:45

3 Answers3

1

You can't initialise it other than

const float test::a = something;

Outside the class (in a single compilation unit). However, you can do what you wrote and that will set the variable to the value you pass.

If you're wanting to set it only on the first time the constructor is entered, you can (but shouldn't) do something like

test::test(int bb, float aa){
    static float _unused = (test::a = aa);

    b=bb;
}

But that doesn't initialise it, it just assigns a value to it, and you'll still have to pass the variable to the constructor every time and nothing will be done with it (unless you give it a default value or something). That is a really terrible design though, it's probably better just to have a static function in the class to set the variable.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • Thank you for the answer. What you would do if you have a an array of these test and you want to be sure that at each manipulation of each element the parameter "a" is kept constant? "a" also is different for each element. I though to this static const but apparently I am wrong... – Fabrizio Jan 27 '12 at 07:13
  • @Fabrizio I don't really understand your question. If `a` is `static`, then it will be assigned only once. If `a` is not static, then only the first object to be constructed will have its `a` set. This is because you're using a `static` variable in the constructor that is only initialised once. – Seth Carnegie Jan 27 '12 at 18:46
0

Static members are not associated with a particular instance, so they will only ever be initialised once. Constructors on the other hand are invoked on a per-instance basis, so it doesn't make sense to do what you're trying to do.

You can, on the other hand, assign a new value to static members in a constructor, as you're doing above, but you still have to actually initialise the static member outside the struct in the normal manner beforehand.

It's worth observing in passing that other languages (e.g. Java) have the concept of a static constructor for exactly this sort of thing - but C++ doesn't.

That said, you might find the following question interesting:

static constructors in C++? I need to initialize private static objects

Community
  • 1
  • 1
Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
0

You can't initialize a static const var inside constructor.

You should initialize at declaration

static const float a = 3.1416f;

Ensure you understand const keyword. And should be integral.

ppk
  • 568
  • 1
  • 7
  • 20
  • 1
    You can only do that with integral constants – Seth Carnegie Jan 26 '12 at 23:26
  • integral ? this works for me under g++ static const float f = 3.14f; So what do you refer ? – ppk Jan 26 '12 at 23:30
  • 1
    Whether it works on one compiler doesn't mean anything, that's a non-standard extension to G++. It doesn't compile on Visual Studio (though that, by itself, doesn't mean anything either, but VS is more conformant in this regard and gets it right). – Seth Carnegie Jan 26 '12 at 23:33
  • No problem, compilers are dastardly pieces of software that can fool you into thinking something's right or wrong when it's really the opposite. I've done it a thousand times. – Seth Carnegie Jan 27 '12 at 00:06