View Single Post
Old Jun 22nd, 2005, 4:31 PM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
Good question! The answer is that the library has no choice but to guarantee that the definition of a static member is performed only once. If a static member is defined multiple times, the behavior is undefined. As with anything falling under the One Definition Rule, the easiest fix is to provide a header with your declarations for inclusion in however many files you want:
#ifndef C_H
#define C_H

class C {
  ...
  static int data;
  ...
};

#endif
And then a source file that is linked with the project once and provides definitions for declared names in the header file:
#include "c.h"

int C::data = 0;
This is a safe and easy way to avoid multiple definitions, especially for situations where an error or warning is not required, such as this one.
Eggbert is offline   Reply With Quote