Global const variables not assigned when constructor is executed





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















MY_GLOBAL_CONST is not assigned when I try to use it in ProblemClass::ProblemClass(). Why? How to fix that? I am working on an old VC6 MFC project.



SomeClass.h



#include "ProblemClass.h"
class SomeClass
{
private:
ProblemClass m_problemClass; //created on the heap

public:
SomeClass();
~SomeClass();
}


ProblemClass.h



class ProblemClass
{
public:
ProblemClass();
~ProblemClass();
}


ProblemClass.cpp



#include "ProblemClass.h"
const CString MY_GLOBAL_CONST = _T("User");//Also tried to put that line in ProblemClass.h without luck
ProblemClass::ProblemClass()
{
CString foo = MY_GLOBAL_CONST; //MFC-Runtime assertion fails, MY_GLOBAL_CONST is not assigned yet
}
ProblemClass::~ProblemClass(){}


Update:



After some further investigation I can confirm that SomeClassis also instantiated in a global context. So, Paul Sanders is absolutely right by saying "happening here is two global initialisers being executed in the wrong order".










share|improve this question




















  • 6





    Do you have a global variable of type ProblemClass or SomeClass in a source file other than ProblemClass.cpp? The order of initialization of globals in different translation units is undefined.

    – Igor Tandetnik
    Nov 23 '18 at 15:53











  • I don't use instances of those classes globally. Only MY_GLOBAL_CONST is global. So far, SomeClass is only used once and ProblemClass is only used in SomeClass

    – OneWorld
    Nov 23 '18 at 16:03






  • 3





    When and where do you use SomeClass? Please try to create a Minimal, Complete, and Verifiable example to show us. Also, if you catch the crash in a debugger, what does the call-stack look like?

    – Some programmer dude
    Nov 23 '18 at 16:06











  • The application is huge. SomeClass appears very early in the call stack while the MY_GLOBAL_CONST isn't assigned yet. We use those global consts (inside namespaces) very often and never had such problems. But we never used them in the startup of the application yet. At least on the modern project, not the VC6 one I have to deal with right now. I will provide the minimal example later.

    – OneWorld
    Nov 23 '18 at 16:13













  • If you don't see main or WinMain on the call stack, then you are in fact creating an instance of SomeClass somewhere as a global variable, or a member thereof.

    – Igor Tandetnik
    Nov 23 '18 at 17:04


















0















MY_GLOBAL_CONST is not assigned when I try to use it in ProblemClass::ProblemClass(). Why? How to fix that? I am working on an old VC6 MFC project.



SomeClass.h



#include "ProblemClass.h"
class SomeClass
{
private:
ProblemClass m_problemClass; //created on the heap

public:
SomeClass();
~SomeClass();
}


ProblemClass.h



class ProblemClass
{
public:
ProblemClass();
~ProblemClass();
}


ProblemClass.cpp



#include "ProblemClass.h"
const CString MY_GLOBAL_CONST = _T("User");//Also tried to put that line in ProblemClass.h without luck
ProblemClass::ProblemClass()
{
CString foo = MY_GLOBAL_CONST; //MFC-Runtime assertion fails, MY_GLOBAL_CONST is not assigned yet
}
ProblemClass::~ProblemClass(){}


Update:



After some further investigation I can confirm that SomeClassis also instantiated in a global context. So, Paul Sanders is absolutely right by saying "happening here is two global initialisers being executed in the wrong order".










share|improve this question




















  • 6





    Do you have a global variable of type ProblemClass or SomeClass in a source file other than ProblemClass.cpp? The order of initialization of globals in different translation units is undefined.

    – Igor Tandetnik
    Nov 23 '18 at 15:53











  • I don't use instances of those classes globally. Only MY_GLOBAL_CONST is global. So far, SomeClass is only used once and ProblemClass is only used in SomeClass

    – OneWorld
    Nov 23 '18 at 16:03






  • 3





    When and where do you use SomeClass? Please try to create a Minimal, Complete, and Verifiable example to show us. Also, if you catch the crash in a debugger, what does the call-stack look like?

    – Some programmer dude
    Nov 23 '18 at 16:06











  • The application is huge. SomeClass appears very early in the call stack while the MY_GLOBAL_CONST isn't assigned yet. We use those global consts (inside namespaces) very often and never had such problems. But we never used them in the startup of the application yet. At least on the modern project, not the VC6 one I have to deal with right now. I will provide the minimal example later.

    – OneWorld
    Nov 23 '18 at 16:13













  • If you don't see main or WinMain on the call stack, then you are in fact creating an instance of SomeClass somewhere as a global variable, or a member thereof.

    – Igor Tandetnik
    Nov 23 '18 at 17:04














0












0








0








MY_GLOBAL_CONST is not assigned when I try to use it in ProblemClass::ProblemClass(). Why? How to fix that? I am working on an old VC6 MFC project.



SomeClass.h



#include "ProblemClass.h"
class SomeClass
{
private:
ProblemClass m_problemClass; //created on the heap

public:
SomeClass();
~SomeClass();
}


ProblemClass.h



class ProblemClass
{
public:
ProblemClass();
~ProblemClass();
}


ProblemClass.cpp



#include "ProblemClass.h"
const CString MY_GLOBAL_CONST = _T("User");//Also tried to put that line in ProblemClass.h without luck
ProblemClass::ProblemClass()
{
CString foo = MY_GLOBAL_CONST; //MFC-Runtime assertion fails, MY_GLOBAL_CONST is not assigned yet
}
ProblemClass::~ProblemClass(){}


Update:



After some further investigation I can confirm that SomeClassis also instantiated in a global context. So, Paul Sanders is absolutely right by saying "happening here is two global initialisers being executed in the wrong order".










share|improve this question
















MY_GLOBAL_CONST is not assigned when I try to use it in ProblemClass::ProblemClass(). Why? How to fix that? I am working on an old VC6 MFC project.



SomeClass.h



#include "ProblemClass.h"
class SomeClass
{
private:
ProblemClass m_problemClass; //created on the heap

public:
SomeClass();
~SomeClass();
}


ProblemClass.h



class ProblemClass
{
public:
ProblemClass();
~ProblemClass();
}


ProblemClass.cpp



#include "ProblemClass.h"
const CString MY_GLOBAL_CONST = _T("User");//Also tried to put that line in ProblemClass.h without luck
ProblemClass::ProblemClass()
{
CString foo = MY_GLOBAL_CONST; //MFC-Runtime assertion fails, MY_GLOBAL_CONST is not assigned yet
}
ProblemClass::~ProblemClass(){}


Update:



After some further investigation I can confirm that SomeClassis also instantiated in a global context. So, Paul Sanders is absolutely right by saying "happening here is two global initialisers being executed in the wrong order".







c++ constructor mfc vc6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 9:05







OneWorld

















asked Nov 23 '18 at 15:51









OneWorldOneWorld

13.2k1765123




13.2k1765123








  • 6





    Do you have a global variable of type ProblemClass or SomeClass in a source file other than ProblemClass.cpp? The order of initialization of globals in different translation units is undefined.

    – Igor Tandetnik
    Nov 23 '18 at 15:53











  • I don't use instances of those classes globally. Only MY_GLOBAL_CONST is global. So far, SomeClass is only used once and ProblemClass is only used in SomeClass

    – OneWorld
    Nov 23 '18 at 16:03






  • 3





    When and where do you use SomeClass? Please try to create a Minimal, Complete, and Verifiable example to show us. Also, if you catch the crash in a debugger, what does the call-stack look like?

    – Some programmer dude
    Nov 23 '18 at 16:06











  • The application is huge. SomeClass appears very early in the call stack while the MY_GLOBAL_CONST isn't assigned yet. We use those global consts (inside namespaces) very often and never had such problems. But we never used them in the startup of the application yet. At least on the modern project, not the VC6 one I have to deal with right now. I will provide the minimal example later.

    – OneWorld
    Nov 23 '18 at 16:13













  • If you don't see main or WinMain on the call stack, then you are in fact creating an instance of SomeClass somewhere as a global variable, or a member thereof.

    – Igor Tandetnik
    Nov 23 '18 at 17:04














  • 6





    Do you have a global variable of type ProblemClass or SomeClass in a source file other than ProblemClass.cpp? The order of initialization of globals in different translation units is undefined.

    – Igor Tandetnik
    Nov 23 '18 at 15:53











  • I don't use instances of those classes globally. Only MY_GLOBAL_CONST is global. So far, SomeClass is only used once and ProblemClass is only used in SomeClass

    – OneWorld
    Nov 23 '18 at 16:03






  • 3





    When and where do you use SomeClass? Please try to create a Minimal, Complete, and Verifiable example to show us. Also, if you catch the crash in a debugger, what does the call-stack look like?

    – Some programmer dude
    Nov 23 '18 at 16:06











  • The application is huge. SomeClass appears very early in the call stack while the MY_GLOBAL_CONST isn't assigned yet. We use those global consts (inside namespaces) very often and never had such problems. But we never used them in the startup of the application yet. At least on the modern project, not the VC6 one I have to deal with right now. I will provide the minimal example later.

    – OneWorld
    Nov 23 '18 at 16:13













  • If you don't see main or WinMain on the call stack, then you are in fact creating an instance of SomeClass somewhere as a global variable, or a member thereof.

    – Igor Tandetnik
    Nov 23 '18 at 17:04








6




6





Do you have a global variable of type ProblemClass or SomeClass in a source file other than ProblemClass.cpp? The order of initialization of globals in different translation units is undefined.

– Igor Tandetnik
Nov 23 '18 at 15:53





Do you have a global variable of type ProblemClass or SomeClass in a source file other than ProblemClass.cpp? The order of initialization of globals in different translation units is undefined.

– Igor Tandetnik
Nov 23 '18 at 15:53













I don't use instances of those classes globally. Only MY_GLOBAL_CONST is global. So far, SomeClass is only used once and ProblemClass is only used in SomeClass

– OneWorld
Nov 23 '18 at 16:03





I don't use instances of those classes globally. Only MY_GLOBAL_CONST is global. So far, SomeClass is only used once and ProblemClass is only used in SomeClass

– OneWorld
Nov 23 '18 at 16:03




3




3





When and where do you use SomeClass? Please try to create a Minimal, Complete, and Verifiable example to show us. Also, if you catch the crash in a debugger, what does the call-stack look like?

– Some programmer dude
Nov 23 '18 at 16:06





When and where do you use SomeClass? Please try to create a Minimal, Complete, and Verifiable example to show us. Also, if you catch the crash in a debugger, what does the call-stack look like?

– Some programmer dude
Nov 23 '18 at 16:06













The application is huge. SomeClass appears very early in the call stack while the MY_GLOBAL_CONST isn't assigned yet. We use those global consts (inside namespaces) very often and never had such problems. But we never used them in the startup of the application yet. At least on the modern project, not the VC6 one I have to deal with right now. I will provide the minimal example later.

– OneWorld
Nov 23 '18 at 16:13







The application is huge. SomeClass appears very early in the call stack while the MY_GLOBAL_CONST isn't assigned yet. We use those global consts (inside namespaces) very often and never had such problems. But we never used them in the startup of the application yet. At least on the modern project, not the VC6 one I have to deal with right now. I will provide the minimal example later.

– OneWorld
Nov 23 '18 at 16:13















If you don't see main or WinMain on the call stack, then you are in fact creating an instance of SomeClass somewhere as a global variable, or a member thereof.

– Igor Tandetnik
Nov 23 '18 at 17:04





If you don't see main or WinMain on the call stack, then you are in fact creating an instance of SomeClass somewhere as a global variable, or a member thereof.

– Igor Tandetnik
Nov 23 '18 at 17:04












2 Answers
2






active

oldest

votes


















4














Try replacing:



const CString MY_GLOBAL_CONST = _T("User");


with:



const TCHAR MY_GLOBAL_CONST  = _T("User");


The latter construct doesn't require any run-time initialisation and MY_GLOBAL_CONST can therefore be relied upon in other initialisation code (because what is surely happening here is two global initialisers being executed in the wrong order).






share|improve this answer
























  • I will give that a try soon! Your conclusion sounds very valid.

    – OneWorld
    Nov 23 '18 at 20:11











  • Note, that this changes runtime behavior, whenever a CString object is created from MY_GLOBAL_CONST. It can no longer invoke the copy c'tor, and needs to re-calculate the length. This means additional runtime overhead, and breakage, in case MY_GLOBAL_CONST contains embedded NUL characters.

    – IInspectable
    Nov 24 '18 at 18:03













  • @IInspectable In the context of the original question, how could MY_GLOBAL_CONST ever contain a NUL character?

    – Paul Sanders
    Nov 24 '18 at 18:21













  • The question is asking about the initialization order of objects with static storage duration. The specific type of the object failing to initialize is not important. Changing the type to solve the issue, however, changes program behavior. If you don't feel like that is an important detail to note, then don't. I don't care, but a future visitor might.

    – IInspectable
    Nov 24 '18 at 18:36



















0














I looks like you missed the static keyword with you declaration. My recipe for gloabl variables which need to be initialized outside of the class.



Header File



class ProblemClass
{
public:
ProblemClass();
~ProblemClass();

private:
static const CString MY_GLOBAL_CONST; // declaration in the header file
}


Source File



const CString ProblemClass::MY_GLOBAL_CONST = _T("HELLO_WORLD"); // Initialization here outside of class

ProblemClass::ProblemClass()
{
CString foo = MY_GLOBAL_CONST; //MFC-Runtime assertion fails, MY_GLOBAL_CONST is not assigned yet
}

// everything else





share|improve this answer



















  • 2





    In the example, MY_GLOBAL_CONST is not a member of ProblemClass.

    – Peter Ruderman
    Nov 23 '18 at 16:15











  • @PeterRuderman OP tried having it as a member, I went this way in light of that. There is no mention of the global not being of part the class as a requirement. It's just one way to go.

    – Chris Mc
    Nov 23 '18 at 16:23











  • I am very aware of constants being part of a class. But in this case, we use global ones. Actually they are wrapped into namespaces

    – OneWorld
    Nov 23 '18 at 20:09












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449634%2fglobal-const-variables-not-assigned-when-constructor-is-executed%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Try replacing:



const CString MY_GLOBAL_CONST = _T("User");


with:



const TCHAR MY_GLOBAL_CONST  = _T("User");


The latter construct doesn't require any run-time initialisation and MY_GLOBAL_CONST can therefore be relied upon in other initialisation code (because what is surely happening here is two global initialisers being executed in the wrong order).






share|improve this answer
























  • I will give that a try soon! Your conclusion sounds very valid.

    – OneWorld
    Nov 23 '18 at 20:11











  • Note, that this changes runtime behavior, whenever a CString object is created from MY_GLOBAL_CONST. It can no longer invoke the copy c'tor, and needs to re-calculate the length. This means additional runtime overhead, and breakage, in case MY_GLOBAL_CONST contains embedded NUL characters.

    – IInspectable
    Nov 24 '18 at 18:03













  • @IInspectable In the context of the original question, how could MY_GLOBAL_CONST ever contain a NUL character?

    – Paul Sanders
    Nov 24 '18 at 18:21













  • The question is asking about the initialization order of objects with static storage duration. The specific type of the object failing to initialize is not important. Changing the type to solve the issue, however, changes program behavior. If you don't feel like that is an important detail to note, then don't. I don't care, but a future visitor might.

    – IInspectable
    Nov 24 '18 at 18:36
















4














Try replacing:



const CString MY_GLOBAL_CONST = _T("User");


with:



const TCHAR MY_GLOBAL_CONST  = _T("User");


The latter construct doesn't require any run-time initialisation and MY_GLOBAL_CONST can therefore be relied upon in other initialisation code (because what is surely happening here is two global initialisers being executed in the wrong order).






share|improve this answer
























  • I will give that a try soon! Your conclusion sounds very valid.

    – OneWorld
    Nov 23 '18 at 20:11











  • Note, that this changes runtime behavior, whenever a CString object is created from MY_GLOBAL_CONST. It can no longer invoke the copy c'tor, and needs to re-calculate the length. This means additional runtime overhead, and breakage, in case MY_GLOBAL_CONST contains embedded NUL characters.

    – IInspectable
    Nov 24 '18 at 18:03













  • @IInspectable In the context of the original question, how could MY_GLOBAL_CONST ever contain a NUL character?

    – Paul Sanders
    Nov 24 '18 at 18:21













  • The question is asking about the initialization order of objects with static storage duration. The specific type of the object failing to initialize is not important. Changing the type to solve the issue, however, changes program behavior. If you don't feel like that is an important detail to note, then don't. I don't care, but a future visitor might.

    – IInspectable
    Nov 24 '18 at 18:36














4












4








4







Try replacing:



const CString MY_GLOBAL_CONST = _T("User");


with:



const TCHAR MY_GLOBAL_CONST  = _T("User");


The latter construct doesn't require any run-time initialisation and MY_GLOBAL_CONST can therefore be relied upon in other initialisation code (because what is surely happening here is two global initialisers being executed in the wrong order).






share|improve this answer













Try replacing:



const CString MY_GLOBAL_CONST = _T("User");


with:



const TCHAR MY_GLOBAL_CONST  = _T("User");


The latter construct doesn't require any run-time initialisation and MY_GLOBAL_CONST can therefore be relied upon in other initialisation code (because what is surely happening here is two global initialisers being executed in the wrong order).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 16:46









Paul SandersPaul Sanders

5,7602621




5,7602621













  • I will give that a try soon! Your conclusion sounds very valid.

    – OneWorld
    Nov 23 '18 at 20:11











  • Note, that this changes runtime behavior, whenever a CString object is created from MY_GLOBAL_CONST. It can no longer invoke the copy c'tor, and needs to re-calculate the length. This means additional runtime overhead, and breakage, in case MY_GLOBAL_CONST contains embedded NUL characters.

    – IInspectable
    Nov 24 '18 at 18:03













  • @IInspectable In the context of the original question, how could MY_GLOBAL_CONST ever contain a NUL character?

    – Paul Sanders
    Nov 24 '18 at 18:21













  • The question is asking about the initialization order of objects with static storage duration. The specific type of the object failing to initialize is not important. Changing the type to solve the issue, however, changes program behavior. If you don't feel like that is an important detail to note, then don't. I don't care, but a future visitor might.

    – IInspectable
    Nov 24 '18 at 18:36



















  • I will give that a try soon! Your conclusion sounds very valid.

    – OneWorld
    Nov 23 '18 at 20:11











  • Note, that this changes runtime behavior, whenever a CString object is created from MY_GLOBAL_CONST. It can no longer invoke the copy c'tor, and needs to re-calculate the length. This means additional runtime overhead, and breakage, in case MY_GLOBAL_CONST contains embedded NUL characters.

    – IInspectable
    Nov 24 '18 at 18:03













  • @IInspectable In the context of the original question, how could MY_GLOBAL_CONST ever contain a NUL character?

    – Paul Sanders
    Nov 24 '18 at 18:21













  • The question is asking about the initialization order of objects with static storage duration. The specific type of the object failing to initialize is not important. Changing the type to solve the issue, however, changes program behavior. If you don't feel like that is an important detail to note, then don't. I don't care, but a future visitor might.

    – IInspectable
    Nov 24 '18 at 18:36

















I will give that a try soon! Your conclusion sounds very valid.

– OneWorld
Nov 23 '18 at 20:11





I will give that a try soon! Your conclusion sounds very valid.

– OneWorld
Nov 23 '18 at 20:11













Note, that this changes runtime behavior, whenever a CString object is created from MY_GLOBAL_CONST. It can no longer invoke the copy c'tor, and needs to re-calculate the length. This means additional runtime overhead, and breakage, in case MY_GLOBAL_CONST contains embedded NUL characters.

– IInspectable
Nov 24 '18 at 18:03







Note, that this changes runtime behavior, whenever a CString object is created from MY_GLOBAL_CONST. It can no longer invoke the copy c'tor, and needs to re-calculate the length. This means additional runtime overhead, and breakage, in case MY_GLOBAL_CONST contains embedded NUL characters.

– IInspectable
Nov 24 '18 at 18:03















@IInspectable In the context of the original question, how could MY_GLOBAL_CONST ever contain a NUL character?

– Paul Sanders
Nov 24 '18 at 18:21







@IInspectable In the context of the original question, how could MY_GLOBAL_CONST ever contain a NUL character?

– Paul Sanders
Nov 24 '18 at 18:21















The question is asking about the initialization order of objects with static storage duration. The specific type of the object failing to initialize is not important. Changing the type to solve the issue, however, changes program behavior. If you don't feel like that is an important detail to note, then don't. I don't care, but a future visitor might.

– IInspectable
Nov 24 '18 at 18:36





The question is asking about the initialization order of objects with static storage duration. The specific type of the object failing to initialize is not important. Changing the type to solve the issue, however, changes program behavior. If you don't feel like that is an important detail to note, then don't. I don't care, but a future visitor might.

– IInspectable
Nov 24 '18 at 18:36













0














I looks like you missed the static keyword with you declaration. My recipe for gloabl variables which need to be initialized outside of the class.



Header File



class ProblemClass
{
public:
ProblemClass();
~ProblemClass();

private:
static const CString MY_GLOBAL_CONST; // declaration in the header file
}


Source File



const CString ProblemClass::MY_GLOBAL_CONST = _T("HELLO_WORLD"); // Initialization here outside of class

ProblemClass::ProblemClass()
{
CString foo = MY_GLOBAL_CONST; //MFC-Runtime assertion fails, MY_GLOBAL_CONST is not assigned yet
}

// everything else





share|improve this answer



















  • 2





    In the example, MY_GLOBAL_CONST is not a member of ProblemClass.

    – Peter Ruderman
    Nov 23 '18 at 16:15











  • @PeterRuderman OP tried having it as a member, I went this way in light of that. There is no mention of the global not being of part the class as a requirement. It's just one way to go.

    – Chris Mc
    Nov 23 '18 at 16:23











  • I am very aware of constants being part of a class. But in this case, we use global ones. Actually they are wrapped into namespaces

    – OneWorld
    Nov 23 '18 at 20:09
















0














I looks like you missed the static keyword with you declaration. My recipe for gloabl variables which need to be initialized outside of the class.



Header File



class ProblemClass
{
public:
ProblemClass();
~ProblemClass();

private:
static const CString MY_GLOBAL_CONST; // declaration in the header file
}


Source File



const CString ProblemClass::MY_GLOBAL_CONST = _T("HELLO_WORLD"); // Initialization here outside of class

ProblemClass::ProblemClass()
{
CString foo = MY_GLOBAL_CONST; //MFC-Runtime assertion fails, MY_GLOBAL_CONST is not assigned yet
}

// everything else





share|improve this answer



















  • 2





    In the example, MY_GLOBAL_CONST is not a member of ProblemClass.

    – Peter Ruderman
    Nov 23 '18 at 16:15











  • @PeterRuderman OP tried having it as a member, I went this way in light of that. There is no mention of the global not being of part the class as a requirement. It's just one way to go.

    – Chris Mc
    Nov 23 '18 at 16:23











  • I am very aware of constants being part of a class. But in this case, we use global ones. Actually they are wrapped into namespaces

    – OneWorld
    Nov 23 '18 at 20:09














0












0








0







I looks like you missed the static keyword with you declaration. My recipe for gloabl variables which need to be initialized outside of the class.



Header File



class ProblemClass
{
public:
ProblemClass();
~ProblemClass();

private:
static const CString MY_GLOBAL_CONST; // declaration in the header file
}


Source File



const CString ProblemClass::MY_GLOBAL_CONST = _T("HELLO_WORLD"); // Initialization here outside of class

ProblemClass::ProblemClass()
{
CString foo = MY_GLOBAL_CONST; //MFC-Runtime assertion fails, MY_GLOBAL_CONST is not assigned yet
}

// everything else





share|improve this answer













I looks like you missed the static keyword with you declaration. My recipe for gloabl variables which need to be initialized outside of the class.



Header File



class ProblemClass
{
public:
ProblemClass();
~ProblemClass();

private:
static const CString MY_GLOBAL_CONST; // declaration in the header file
}


Source File



const CString ProblemClass::MY_GLOBAL_CONST = _T("HELLO_WORLD"); // Initialization here outside of class

ProblemClass::ProblemClass()
{
CString foo = MY_GLOBAL_CONST; //MFC-Runtime assertion fails, MY_GLOBAL_CONST is not assigned yet
}

// everything else






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 16:13









Chris McChris Mc

32225




32225








  • 2





    In the example, MY_GLOBAL_CONST is not a member of ProblemClass.

    – Peter Ruderman
    Nov 23 '18 at 16:15











  • @PeterRuderman OP tried having it as a member, I went this way in light of that. There is no mention of the global not being of part the class as a requirement. It's just one way to go.

    – Chris Mc
    Nov 23 '18 at 16:23











  • I am very aware of constants being part of a class. But in this case, we use global ones. Actually they are wrapped into namespaces

    – OneWorld
    Nov 23 '18 at 20:09














  • 2





    In the example, MY_GLOBAL_CONST is not a member of ProblemClass.

    – Peter Ruderman
    Nov 23 '18 at 16:15











  • @PeterRuderman OP tried having it as a member, I went this way in light of that. There is no mention of the global not being of part the class as a requirement. It's just one way to go.

    – Chris Mc
    Nov 23 '18 at 16:23











  • I am very aware of constants being part of a class. But in this case, we use global ones. Actually they are wrapped into namespaces

    – OneWorld
    Nov 23 '18 at 20:09








2




2





In the example, MY_GLOBAL_CONST is not a member of ProblemClass.

– Peter Ruderman
Nov 23 '18 at 16:15





In the example, MY_GLOBAL_CONST is not a member of ProblemClass.

– Peter Ruderman
Nov 23 '18 at 16:15













@PeterRuderman OP tried having it as a member, I went this way in light of that. There is no mention of the global not being of part the class as a requirement. It's just one way to go.

– Chris Mc
Nov 23 '18 at 16:23





@PeterRuderman OP tried having it as a member, I went this way in light of that. There is no mention of the global not being of part the class as a requirement. It's just one way to go.

– Chris Mc
Nov 23 '18 at 16:23













I am very aware of constants being part of a class. But in this case, we use global ones. Actually they are wrapped into namespaces

– OneWorld
Nov 23 '18 at 20:09





I am very aware of constants being part of a class. But in this case, we use global ones. Actually they are wrapped into namespaces

– OneWorld
Nov 23 '18 at 20:09


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449634%2fglobal-const-variables-not-assigned-when-constructor-is-executed%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy