12

Many other questions deal with how to allocate a variable by declaring it in a header file and defining it (allocating) in a .cpp file.

What I want to do is not use any .cpp files for my class, and to define all functions as inline (in the header file). The problem that I run into is how to define static member variables so that even when the .h file is included in multiple compilation units I don't get the "first defined here" linker error.

I'm open to preprocessor hacks, etc. if it gets the job done. I just want to avoid any .cpp files.

If it matters I'm using GCC.

Community
  • 1
  • 1
srlm
  • 3,186
  • 2
  • 27
  • 40
  • You simply declare your variable as `static`. A new instance of the variable will be created in each translation unit you include the header file in, but at link time, there will be no conflicts since the objects have static linkage... –  Aug 03 '13 at 10:32
  • No, static data members have external linkage. – jrok Aug 03 '13 at 10:34
  • "defining it (allocating)": **defining** and **allocating** are two different things. – Pete Becker Aug 03 '13 at 10:50
  • @cmaster - allocating is **part** of defining, but they are not the same thing. – Pete Becker Aug 03 '13 at 12:47
  • @PeteBecker it's true that the words have a different meaning, however, for variables they are essentially synonyms, so there's nothing wrong with "defining it (allocating)". But this discussion leads to nothing, so I suggest we both just delete our comments. – cmaster - reinstate monica Aug 03 '13 at 13:01

3 Answers3

14

You can abuse the singleton pattern if you really must avoid any .cpp files:

class Foo {
    public:
        static Bar& getMyStatic() {
            static Bar bar;
            return bar;
        };
};

This works because now the variable is a static variable inside a function, and static has a different meaning within a function context than within a class context. And for functions, the linker does recognize multiple identical definitions and throws away the copies.

But, of course, I would strongly advise against avoiding .cpp files: It means that you get into a situation where you have to build the entire program, or at least large parts of it, in one big piece. Every change you do will necessitate a complete rebuilt which slows down your change-compile-test cycle significantly. For very small projects that might not be a problem, but it is for medium to large ones.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
  • This seems to be the best (and only) way to avoid putting static variables in a .cpp file. As for compile time: my compiled code is limited to 32KB, so that puts an upper bound on how long it will take. – srlm Dec 02 '13 at 15:56
0

With static variables you have to put in a .cpp file to avoid the possibility of multiple static variables when the intention is to have just the one. Besides it is not a good idea to have large inline methods as it is only a hint to the compiler but also makes compilation take longer (you change some of those functions in development and then lots of dependent files will need to get compiled!)

However if you do not want lots of .cpp files with just a few statics in it why not have just one file to store them in.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
-1

As long as you only include that header file once in your whole project, you'll be OK. However, that's a pretty strong requirement, and can be difficult to make others adhere to.

You could have a static variable, but that means you have more than one for the entire program, which may or may not matter (bear in mind that you can't change it in the future, so you may have what's known as a "latent bug" - you change some other code, and all of a sudden you have created a new bug, because the variable isn't ONE variable).

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • 1
    Trouble is that it may not start off that it matters but in the future it may do and you inadvertently introduce a bug – Ed Heal Aug 03 '13 at 10:36