Reputation: 338
I have this code here:
#ifndef BT_ORDER_HPP
#define BT_ORDER_HPP
class Order {
public:
/*....*/
private:
static unsigned long long id_generator;
const unsigned long long id;
};
unsigned long long Order::id_generator = 0;
#endif//BT_ORDER_HPP
it does not compile if the header file is included in multiple compilation units because, as far as I understood, the static variable is defined in all the compilation units. But that does not make sense to me, because I am using ifndef guards; I thought that the compiler would check if the macro was already defined and in that case omit the code that is guarded by the block, definition of the static variable included. This specific class I am making has a lot of inlined, simple functions, so creating a source .cpp file for this would solve the problem, and it's the standard way to go about this, but in this case i feel like it's unnecessary, since I would have to make a whole new file with only two lines of code:
#include "order.hpp"
unsigned long long Order::id_generator = 0;
Can anyone explain what is going on here and offer an elegant solution?
Upvotes: 2
Views: 93