Using the value of a static member variable in a constructor, initializing the value of the static member variable outside the class. If it is of type int, the value is initialized and accessible, but if it is of type std:: string
or other non primitive types, the value does not exist. Why is this.
Here are my header files and CPP files:
namespace hiveGL
{
namespace core
{
class CRenderEnvConfig : public hiveConfig::CHiveConfig
{
public:
CRenderEnvConfig();
~CRenderEnvConfig() = default;
std::pair<std::uint32_t, std::uint32_t> getWindowSize() const;
void validate(const CRenderEnvConfig *vDefaultConfig);
static const std::string ATTRIBUTE_WINDOW_WIDTH;
static const std::string ATTRIBUTE_WINDOW_HEIGHT;
static const std::string ATTRIBUTE_WINDOW_SYSTEM;
static const std::string ATTRIBUTE_WINDOW_TILE;
static const std::string ATTRIBUTE_GL_PROFILE;
static const std::string ATTRIBUTE_GL_VERSION;
static const std::string SUBCONFIG_RENDER_ENV;
static const int myNumber;
static const std::string myString;
private:
static const std::map<std::string, hiveConfig::EConfigDataType> m_Attributes;
void __defineAttributesV() override;
void __loadDefaultConfigV() override;
template<typename T>
void __validateAttribute(const std::string& vName, const std::function<bool(const T&)>& vValidator, const CRenderEnvConfig* vDefaultConfig);
};
}
}
const int CRenderEnvConfig::myNumber = 20;
const std::string CRenderEnvConfig::myString = "hello";
CRenderEnvConfig::CRenderEnvConfig()
{
std::cout << myNumber << "n";
std::cout << myString << "n";
__defineAttributesV();
}
below is result:
What is the reason why static member values cannot be used in constructors