I have a loooong debugging session behind me! I finally found the reason for the problem and now would like to know, if it is a bug in my code or not standardconformant behavour of the compiler(s) or not a bug at all and just normal behavior:
This simple sample program illustrates the problem. Please use separate header (.h) and code (.cpp) files, because it plays a role in the problem...
- /**************************************
-
* Module: TCStaticmember.h
-
**************************************/
-
#if !defined(TCStaticmember_h)
-
#define TCStaticmember_h
-
#include <string>
-
-
class TCStaticmember
-
{
-
public:
-
static std::string staticMember;
-
static int staticMemberPOD;
-
-
protected:
-
private:
-
};
- //move the definition of staticMember here to get it working
-
#endif
-
-
/**************************************
-
* Module: TCStaticmember.cpp
-
**************************************/
-
#include "TCStaticmember.h"
-
- //move the following definition to TCStaticmember.h to get it working
-
std::string TCStaticmember::staticMember="I'm the value of a static std::string variable!";
-
int TCStaticmember::staticMemberPOD=2;
-
-
/**************************************
-
* Module: TStaticMemberUser.h
-
**************************************/
-
#if !defined(TStaticMemberUser_h)
-
#define TStaticMemberUser_h
-
-
#include "TCStaticmember.h";
-
#include <string>
-
-
class TStaticMemberUser
-
{
-
public:
-
TStaticMemberUser();
-
-
std::string mystring;
-
int myint;
-
-
protected:
-
private:
-
};
-
-
#endif
-
-
/**************************************
-
* Module: TStaticMemberUser.cpp
-
**************************************/
-
#include "TCStaticmember.h"
-
#include "TStaticMemberUser.h"
-
-
TStaticMemberUser::TStaticMemberUser()
-
{
-
mystring = TCStaticmember::staticMember;
-
myint = TCStaticmember::staticMemberPOD;
-
}
-
-
/**************************************
-
* Module: main.cpp
-
**************************************/
-
#include "stdio.h"
-
#include "TStaticMemberUser.h"
-
TStaticMemberUser StaticMemberUser;
-
int main(int argc, char* argv[])
-
{
-
printf ("The value of the static std::string variable is: %s\n",StaticMemberUser.mystring.c_str());
-
printf ("The value of the static POD (int) is: %d\n",StaticMemberUser.myint);
-
return 0;
-
}
I built this program using 3 different compilers/linkers:
Borland compiler and linker included in C++ Builder 6.0
Microsoft compiler and linker included in Visual C++ 6.0
GNU compiler and linker (gcc version 3.4.2 (mingw-special))
The GNU compiler builds the program without any problems, but does never enter the main function in debugging mode and crashes in normal mode (GPF)
The Borland and Microsoft Compiler both generate the following output:
- The value of the static std::string variable is:
-
The value of the static POD (int) is: 2
Why is the std::string variable not initialized, whereas the int is correctly set to 2?
In Borland C++ the std::string variable even can be in an undefined state leading to occasional system crashes at startup (the reason for my debugging session...).
The "solution" for the problem is strange in my view: I have to move the definition of the std::string variable from the
TCStaticMember.cpp file to the
TCStaticMember.h file (the locations to delete/paste are marked with bold comments in the code). With Microsoft I then have to use the -F (Force) linker flag because of duplicate definitions, but everything works as expected then...
Borland even does not complain about that and the output is as expected:
- The value of the static std::string variable is: I'm the value of a static std::string variable!
-
The value of the static POD (int) is: 2
I would really appreciate if someone could answer the question, if me, the compilers, the STL implementers or the standard definers did something wrong...
Thanks in advance...