1

I have a static variable in my header. Example:

Header file:

class Fruits{
    public:
        static int colour;
}

At the cpp file after including the header, is it better to write:

int Fruits::colour=1;

or

int Fruits::colour(1);

Someone told me that the first is not an initialization of the variable but a declaration of another. What is the correct way and place to set the initialization?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kyrpav
  • 756
  • 1
  • 13
  • 43
  • 2
    Please don't tag C++ questions `c`. – Fred Foo Feb 20 '13 at 15:09
  • possible duplicate of [Initializing private static members](http://stackoverflow.com/questions/185844/initializing-private-static-members) – egrunin Feb 20 '13 at 15:12
  • This question is too broad. First you need this http://stackoverflow.com/questions/1051379/is-there-a-difference-in-c-between-copy-initialization-and-direct-initializati (and there are other questions that answer this as well, particularly if you don't know these terms), then you need the one posted as dupe above. – djechlin Feb 20 '13 at 15:15
  • sorry larsmans but look at the answer of that person.why shouldn't i tag it to c and c++.Someone can know the difference and be usefull in order to learn.Also arduino as the manual says it is a c/c++ programmable.Anyway – kyrpav Feb 20 '13 at 15:20
  • sorry djechlin i read several post in the search but i did not find these so i posted the question. – kyrpav Feb 20 '13 at 15:22
  • 1
    @kyrpav: The question isn't about C because C doesn't have classes, and therefore doesn't have static member variables. In C you would have to use the first style of initialisation, since the second isn't valid. – Mike Seymour Feb 20 '13 at 15:34
  • @kyrpav first google phrase "difference between initialize syntax c++" gives an SO link explaining the difference generally. And AFAIK there's nothing special about whether it's static or not in this case. – djechlin Feb 20 '13 at 15:35

7 Answers7

4

When C++ was being designed they decided that, to keep consistency with earlier code, type X = y; would be considered as equivalent to type X(y); for built-in types. The two examples of static initialisation you give are therefore treated as utterly identical by the compiler and are just different ways of writing the same thing.

For classes, it becomes more complicated. In many cases, type X = y and type X(y) are interchangeable, however, there are circumstances in which they will result in different results. This is discussed, at length, in the answers to this question.

Community
  • 1
  • 1
Jack Aidley
  • 19,439
  • 7
  • 43
  • 70
  • one more question.as i asked is that initialization the initialization of a new variable or the initialization of the same static that i have declared in the header file cause some people confused me. – kyrpav Feb 20 '13 at 15:30
  • They're not "utterly identical" for all types - `type X = y;` will fail to compile for unmovable types, while `type X(y);` will work for all (suitably convertible) types. – Mike Seymour Feb 20 '13 at 15:31
  • so can you answer if this is the declaration and the definition of a new variable different than the one i have declared in the header? Also what are the unmovable types? – kyrpav Feb 20 '13 at 15:36
  • @kyrpav: Both are definitions of the static variable you declared in the class. The difference is the way they are initialised. The first form (copy or move initialisation) creates a temporary object and then initialises `colour` by copying or moving that. It will fail if it is a class type with no available copy or move constructor; that's what I mean by an "unmovable type". The second form (direct initialisation) does not create a temporary, and does not need to copy or move anything. – Mike Seymour Feb 20 '13 at 15:48
  • 1
    @MikeSeymour: Jack mentioned "for built-in types", which are movable as I believe – Andriy Tylychko Feb 20 '13 at 16:40
  • @MikeSeymour: You're correct. However, I did specify "built-in types"; and these two statements are _identical_ for built-in types. – Jack Aidley Feb 20 '13 at 16:43
  • Sorry, I missed the "for built-in types" part. It might be worth stressing that a bit more, since that's an important qualification. – Mike Seymour Feb 20 '13 at 16:47
  • @MikeSeymour: I have expanded accordingly. – Jack Aidley Feb 20 '13 at 16:53
3

They are identical, except that the first one uses copy-initialization syntax (see Paragraph 8.5/14 of the C++11 Standard):

T a = b;

While the second one uses direct-initialization syntax (see Paragraph 8.5/15 of the C++11 Standard):

T a(b); // C++11 also supports T a{b};

For non-class types, the two are equivalent.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
2

Those two lines of code are equivalent, and both are initialization.

djechlin
  • 59,258
  • 35
  • 162
  • 290
2

For simple types like int, both are equivalent.

For class types, there is a difference: the first will copy- or move-initialise from a temporary object, while the second will directly initialise the named variable. In practise, the copy or move will be elided and both will do exactly the same thing; but the first will fail to compile if the class is neither copyable nor movable.

So it's better to use the second for class types; and, if you like consistency, you might prefer to use it for all types.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

" but a declaration of another"

Yes, it is a declaration of another only if you are defining it multiple times in different headers. But defining it only in a file in which it is declared, then it is the same.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arpit
  • 12,767
  • 3
  • 27
  • 40
0

Everything seems fine with the first solution. You can use this one. As far as I can see the other way is not used.

See here.

bash.d
  • 13,029
  • 3
  • 29
  • 42
0

Both are equivalent in operation, although

int Fruits::colour(1); in this case, if colour was an object of a different class then it would call the constructor (or if another object of the same class was supplied, it would call the copy constructor).

Since the type is an int which is one of the basic types, then, they're both equivalent and equally valid.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78