static member initialization order fiasco
-
in C++ yes... but in general OOP concepts, i'm not sure every languages know what a namespace is, unless classes scopes.
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
toxcct wrote:
in C++ yes
But we are on a C++ board right ;) ?
Cédric Moonen Software developer
Charting control -
toxcct wrote:
in C++ yes
But we are on a C++ board right ;) ?
Cédric Moonen Software developer
Charting controlCedric Moonen wrote:
But we are on a C++ board right
oops, you're right, i thought a moment that we were in the lounge... :doh::laugh:
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
Hi, I think I experienced the issue described here with one of my classes : http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.15 I realized that my static member ConfigManager::ConfigFile isn't initialized when I first call a static member function ConfigManager::GetConfigString using it. Could you have a look at my code below and suggest something to avoid this. I'm not really happy with the solution from the faq because it uses "new" to create an object instance that I can't "delete" because my class is static (no destructor). Thanks ! ConfigManager.h
#pragma once #define DEFAULT_PROFILE CString("C:\\default.ini") #define DEFAULT_INT -1 #define DEFAULT_STRING CString(""); class ConfigManager { public: static CString GetConfigFile(); static void SetConfigFile(CString& p); static void SetConfig(CString section, CString key, CString value); static CString GetConfigString(CString section, CString key, CString def = DEFAULT_STRING); static int GetConfigInt(CString section, CString key, int def = DEFAULT_INT); private: static CString ConfigFile; };
ConfigManager.cpp#include "StdAfx.h" #include "ConfigManager.h" CString ConfigManager::ConfigFile = DEFAULT_PROFILE; void ConfigManager::SetConfig(CString section, CString key, CString value) { WritePrivateProfileString(section, key, value, ConfigManager::ConfigFile); } CString ConfigManager::GetConfigString(CString section, CString key, CString def) { CString result(_T(" "),512); GetPrivateProfileString(section, key, def, result.GetBuffer(), 512, ConfigManager::ConfigFile); result.ReleaseBuffer(); return result; } int ConfigManager::GetConfigInt(CString section, CString key, int def) { return GetPrivateProfileInt(section, key, def, ConfigManager::ConfigFile); } CString ConfigManager::GetConfigFile() { return ConfigManager::ConfigFile; } void ConfigManager::SetConfigFile(CString& p) { ConfigManager::ConfigFile = p; }
If your ConfigFile value isn't ever going to be changed, then you can do this
class ConfigManager
{
.
.
.
private:
static const CString ConfigFile = DEFAULT_PROFILE;
}; -
Steve S wrote:
CString ConfigManager::ConfigFile(DEFAULT_PROFILE);
Just tried this, there no change. ConfigManager::ConfigFile is not initialized upon first call... then later upon second call it is.
-
If your ConfigFile value isn't ever going to be changed, then you can do this
class ConfigManager
{
.
.
.
private:
static const CString ConfigFile = DEFAULT_PROFILE;
};Weiye Chen wrote:
private: static const CString ConfigFile = DEFAULT_PROFILE;
this is not allowed in C++ (you are showing a C# solution) he must initialize its static member outise of the class
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
I don't have an answer to your problem but may I ask why you are using static methods only ? Maybe a singleton pattern is more suited to your needs ? (You'll have only one instance of the class). I don't really see the advantage of using only static members.
Cédric Moonen Software developer
Charting controlCedric Moonen wrote:
I don't really see the advantage of using only static members.
Well every method of my class is standalone, it doesn't need any prerequisite to deliver a result. For me the point in having a class instance is to expect a behaviour depending on the past life of the instance. Here I don't think I need to create any (even a single) instance of the class. Eventually I'll try your idea because it is more likely to work but I think it is simplier and nicer to have ConfigMananger::GetConfigString(....) rather than ConfigManager::GetInstance()->GetConfigString(...)
-
Weiye Chen wrote:
private: static const CString ConfigFile = DEFAULT_PROFILE;
this is not allowed in C++ (you are showing a C# solution) he must initialize its static member outise of the class
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
Hi, I think I experienced the issue described here with one of my classes : http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.15 I realized that my static member ConfigManager::ConfigFile isn't initialized when I first call a static member function ConfigManager::GetConfigString using it. Could you have a look at my code below and suggest something to avoid this. I'm not really happy with the solution from the faq because it uses "new" to create an object instance that I can't "delete" because my class is static (no destructor). Thanks ! ConfigManager.h
#pragma once #define DEFAULT_PROFILE CString("C:\\default.ini") #define DEFAULT_INT -1 #define DEFAULT_STRING CString(""); class ConfigManager { public: static CString GetConfigFile(); static void SetConfigFile(CString& p); static void SetConfig(CString section, CString key, CString value); static CString GetConfigString(CString section, CString key, CString def = DEFAULT_STRING); static int GetConfigInt(CString section, CString key, int def = DEFAULT_INT); private: static CString ConfigFile; };
ConfigManager.cpp#include "StdAfx.h" #include "ConfigManager.h" CString ConfigManager::ConfigFile = DEFAULT_PROFILE; void ConfigManager::SetConfig(CString section, CString key, CString value) { WritePrivateProfileString(section, key, value, ConfigManager::ConfigFile); } CString ConfigManager::GetConfigString(CString section, CString key, CString def) { CString result(_T(" "),512); GetPrivateProfileString(section, key, def, result.GetBuffer(), 512, ConfigManager::ConfigFile); result.ReleaseBuffer(); return result; } int ConfigManager::GetConfigInt(CString section, CString key, int def) { return GetPrivateProfileInt(section, key, def, ConfigManager::ConfigFile); } CString ConfigManager::GetConfigFile() { return ConfigManager::ConfigFile; } void ConfigManager::SetConfigFile(CString& p) { ConfigManager::ConfigFile = p; }
You could put the constant in an anonymous namespace at the top of your implementation file, rather than in the class. You could also have
GetConfigFile()
set the file name if it hasn't been set, and always call it, rather than use the filename directly - that way it would always be initialised before it was used Cedric is right though. Since your class contains state, it is better implemented as a singleton, rather than a bunch of static methods and a static data member.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Cedric Moonen wrote:
I don't really see the advantage of using only static members.
Well every method of my class is standalone, it doesn't need any prerequisite to deliver a result. For me the point in having a class instance is to expect a behaviour depending on the past life of the instance. Here I don't think I need to create any (even a single) instance of the class. Eventually I'll try your idea because it is more likely to work but I think it is simplier and nicer to have ConfigMananger::GetConfigString(....) rather than ConfigManager::GetInstance()->GetConfigString(...)
Yes, but... Why use a class then ? :) Ok, I know that you don't want to use global functions maybe but it's still clearer to declare an instance of your class first and then use the functions. Don't you think ? Otherwise, this doesn't make a lot of sense.
Cédric Moonen Software developer
Charting control -
Yes, but... Why use a class then ? :) Ok, I know that you don't want to use global functions maybe but it's still clearer to declare an instance of your class first and then use the functions. Don't you think ? Otherwise, this doesn't make a lot of sense.
Cédric Moonen Software developer
Charting control -
You could put the constant in an anonymous namespace at the top of your implementation file, rather than in the class. You could also have
GetConfigFile()
set the file name if it hasn't been set, and always call it, rather than use the filename directly - that way it would always be initialised before it was used Cedric is right though. Since your class contains state, it is better implemented as a singleton, rather than a bunch of static methods and a static data member.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
Ryan Binns wrote:
You could also have GetConfigFile() set the file name if it hasn't been set
Yes I had this idea already... but did not succeed at first... I had a look again after seeing your message and it seems the following code works :
CString ConfigManager::GetConfigFile() { static bool bInit = false; if (!bInit) { ConfigManager::ConfigFile = DEFAULT_FILE; bInit = true; } return ConfigManager::ConfigFile; }
Thanks -
Ryan Binns wrote:
You could also have GetConfigFile() set the file name if it hasn't been set
Yes I had this idea already... but did not succeed at first... I had a look again after seeing your message and it seems the following code works :
CString ConfigManager::GetConfigFile() { static bool bInit = false; if (!bInit) { ConfigManager::ConfigFile = DEFAULT_FILE; bInit = true; } return ConfigManager::ConfigFile; }
ThanksYep, that will do it. I would recommend either putting the variable in an anonymous namespace or making your class a singleton though. It's not much of a hack.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"