Sign in to follow this  
mazelle

Workaround for this

Recommended Posts

mazelle    106
static const variables used in constructors have no value. What is the best way to fix it without using macros?
// header file
public:

static const std::string SOME_CONSTANT;

/**
 * Constructor
 */
Object();

// implementation file
const std::string Object::SOME_CONSTANT( "12345" );

Object::Object()
{
  doSomething( SOME_CONSTANT ); // SOME_CONSTANT has no value here.
}

Share this post


Link to post
Share on other sites
Plasmarobo    100
No, that's not true. The code compiles and runs fine for me (with modifications to make it make sense). You should use source tags, etc, etc.
I don't see a problem...

Share this post


Link to post
Share on other sites
mazelle    106
Now I've replicated it. Try this:


// Test.h

class Test
{
public:
static const std::string SOME_CONSTANT;

Test();
};




// Test.cpp

const std::string Test::SOME_CONSTANT( "12345" );

Test::Test()
{
OutputDebugString( SOME_CONSTANT );
}




// main

Test object; // constructor is invoked before the constant gets its value
// so nothing gets printed

void main()
{
}




void main()
{
Test object; // constant gets value first before constructor gets invoked
// "12345" gets printed
}



What do you guys think? Why is it behaving this way?

Share this post


Link to post
Share on other sites
TheUnbeliever    963
That works exactly as I'd expect (MSVC++ 2008 EE) - both variations output "12345".

However, you're not copying and pasting code, because even your minimal example code is missing necessary includes and passing an invalid parameter to OutputDebugString, meaning the problem could be elsewhere.

EDIT: For reference:


#include <string>
#include <windows.h>

class Test
{
public:
static const std::wstring SOME_CONSTANT;

Test();
};

const std::wstring Test::SOME_CONSTANT(L"12345");

Test::Test()
{
OutputDebugString(SOME_CONSTANT.c_str());
OutputDebugString(L"\n");
}

Test object1;
int main()
{
Test object2;
}


(Scroll to bottom)
'test.exe': Loaded 'C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\test\Debug\test.exe', Symbols loaded.
'test.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', Exports loaded.
'test.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', Exports loaded.
'test.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcp90d.dll', Exports loaded.
'test.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcr90d.dll', Exports loaded.
'test.exe': Loaded 'C:\Program Files\Common Files\LogiShrd\LVMVFM\LVPrcInj.dll', Exports loaded.
'test.exe': Loaded 'C:\WINDOWS\system32\user32.dll', Exports loaded.
'test.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', Exports loaded.
'test.exe': Loaded 'C:\WINDOWS\system32\lpk.dll', Exports loaded.
'test.exe': Loaded 'C:\WINDOWS\system32\usp10.dll', Exports loaded.
'test.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', Exports loaded.
'test.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', Exports loaded.
'test.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', Exports loaded.
'test.exe': Loaded 'C:\Program Files\Sophos\Sophos Anti-Virus\sophos_detoured.dll', Exports loaded.
'test.exe': Loaded 'C:\WINDOWS\system32\psapi.dll'
'test.exe': Unloaded 'C:\Program Files\Sophos\Sophos Anti-Virus\sophos_detoured.dll'
'test.exe': Unloaded 'C:\WINDOWS\system32\psapi.dll'
The thread 'Win32 Thread' (0x25ac) has exited with code 8388608 (0x800000).
12345
12345
The program '[9824] test.exe: Native' has exited with code 0 (0x0).

Share this post


Link to post
Share on other sites
mazelle    106
I've pasted the minimal code of course just to show how I replicated it.

I'm using MSVC++ 2005. Could that be the problem? I've run the debugger to actually see how the code runs. What I've described is what is happening.

Share this post


Link to post
Share on other sites
jpetrie    13104
Post the actual code you're using. Whatever you've deemed unneccessary is actually part of the problem (see your modification to the OutputDebugString call that renders it uncompilable since the function does not accept std::string). Copy and paste your example exactly as it compiles on your machine to produce the behavior.

My best guess without seeing the real code is that you're running into a static initialization order between two unrelated types across translation units.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this