Imagine i have such library:
Library.h
class DLLEXPORT LibraryClass
{
private:
int _id;
static int _last_id;
public:
LibraryClass();
bool operator == (const LibraryClass t)
{return _id == t._id;}
};
Library.cpp
#include "Library.h"
int LibraryClass::_last_id = 0;
LibraryClass::LibraryClass()
_id(_last_id)
{
++_last_id;
}
Will it work correct? I’m getting C4835 warning in Visual Studio, but seems it works. Does anybody know how it will work on other compilers (i’m interested in linux gcc and mac gcc)? Is there another “valid” implementation for such pattern?
Your syntax is fine and this shouldn’t cause any issues in your code; I don’t think you’d see any warnings on a UNIX/MAC system while compiling this (except for the fact that you’re doing a DLL export which is windows oriented). I belive you’re just seeing fallout of managed C++.
From MSDN:
Your static data member will be in the initialized test segment of your program when compiled on unix. It is guaranteed to be initialized to the value you provided prior to execution, so it will start at 0, and it will be completely usable in your constructor by the time it is invoked.