2

I don't think this is a duplicate because other questions ask why it's necessary, not how I can avoid writing it twice.

Often with classes I'll introduce member variables and then, for whatever reason, remove them shortly after if I don't like them.

With a non-static member variable I can simply add a member to the header file and use it in my code immediately. With a static member variable I have to do the following:

  • Add the new member to the class definition, e.g. static int a;.
  • Copy the new member declaration.
  • Go to a .cpp file (any .cpp file will do if I understand right).
  • Paste the variable in the file.
  • Remove the static keyword.
  • Add the class namespace/scope after the type and before the variable name.

All of this makes me want to just make all of my classes instantiable and do everything through an object, even if it wouldn't make sense.

So even if this is just a requirement of the language and there is no way around it, is there a way to cut back on the repetition by using macros in some way?

Also I was thinking if maybe it mightn't be simpler to just have one .cpp file containing all of these static member variable definitions. Seeing as though (I've heard) static member variables are basically global variables accessed through a class namespace, is this a better idea than doing it in each corresponding .cpp file?

Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • If a variable doesn't have a definition, how could it exist? What you put in the class is basically just a declaration, while you put the definition in the source file. – Some programmer dude Nov 23 '16 at 08:02
  • By the way, if you don't [ODR use](http://stackoverflow.com/questions/19630570/what-does-it-mean-to-odr-use-something) the static member variable, you don't need the definition. – Some programmer dude Nov 23 '16 at 08:05
  • "any .cpp file will do if I understand right": any .cpp file which includes the variable declaration. "Replace the static keyword with the type of the variable": no, the type should also be present in the declaration, you don't replace static with the type, you just remove static – rocambille Nov 23 '16 at 08:26

2 Answers2

3

I'm going to provide a solution for integral types (since you highlight those types in your question):

struct Foo
{
    enum {Value = 123;}
};

Value can be used as an integral constant, is the same for all instances of Foo (rather like a static) and does not require definition in a source file. Note though that &Value makes no sense, i.e. there's no such thing as a pointer to an enumeration value.

You can do things with constexpr in later C++ standards (C++11 onwards), but my way is arguably simpler and is also an important metaprogramming technique.

(If this is not sufficient for what you want then please downvote and I'll remove.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Wow, that's actually a neat little trick. You can't take the address of a static class variable or a global variable also, can you? – Zebrafish Nov 23 '16 at 08:14
  • @TitoneMaurice: You *can* take the address of a static class variable, or a global variable. – Bathsheba Nov 23 '16 at 08:22
1

A possibility is too use static in function scope, something like:

struct C {
    static int& a() {
        static int a_instance = 42;
        return a_instance;
    }
};
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Thank you, that's very good. Maybe you can macro that up into something shorter. – Zebrafish Nov 23 '16 at 09:12
  • You can probably do a MACRO to make it shorter, but I don't want to promote MACRO usage for that kind of thing. – Jarod42 Nov 23 '16 at 09:16
  • Yeah definitely. I've been learning about macros and they are a minefield from the looks of it. – Zebrafish Nov 23 '16 at 09:21
  • I'm just wondering, wouldn't it have been super easy for the compiler to parse through the members of the class, look for which members are static variables, and simply define them in a .cpp? I imagine it does something a lot more complex than that when instantiating template types. Even when Visual Studio gives you the option of creating a function definition from the signature in a .cpp. Anyway, I won't dwell on this longer. – Zebrafish Nov 23 '16 at 09:28