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;
}
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 SomeClass
is 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
add a comment |
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 SomeClass
is 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
6
Do you have a global variable of typeProblemClass
orSomeClass
in a source file other thanProblemClass.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. OnlyMY_GLOBAL_CONST
is global. So far,SomeClass
is only used once andProblemClass
is only used inSomeClass
– OneWorld
Nov 23 '18 at 16:03
3
When and where do you useSomeClass
? 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 theMY_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 seemain
orWinMain
on the call stack, then you are in fact creating an instance ofSomeClass
somewhere as a global variable, or a member thereof.
– Igor Tandetnik
Nov 23 '18 at 17:04
add a comment |
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 SomeClass
is 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
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 SomeClass
is 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
c++ constructor mfc vc6
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 typeProblemClass
orSomeClass
in a source file other thanProblemClass.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. OnlyMY_GLOBAL_CONST
is global. So far,SomeClass
is only used once andProblemClass
is only used inSomeClass
– OneWorld
Nov 23 '18 at 16:03
3
When and where do you useSomeClass
? 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 theMY_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 seemain
orWinMain
on the call stack, then you are in fact creating an instance ofSomeClass
somewhere as a global variable, or a member thereof.
– Igor Tandetnik
Nov 23 '18 at 17:04
add a comment |
6
Do you have a global variable of typeProblemClass
orSomeClass
in a source file other thanProblemClass.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. OnlyMY_GLOBAL_CONST
is global. So far,SomeClass
is only used once andProblemClass
is only used inSomeClass
– OneWorld
Nov 23 '18 at 16:03
3
When and where do you useSomeClass
? 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 theMY_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 seemain
orWinMain
on the call stack, then you are in fact creating an instance ofSomeClass
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
add a comment |
2 Answers
2
active
oldest
votes
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).
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 aCString
object is created fromMY_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 caseMY_GLOBAL_CONST
contains embedded NUL characters.
– IInspectable
Nov 24 '18 at 18:03
@IInspectable In the context of the original question, how couldMY_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
add a comment |
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
2
In the example,MY_GLOBAL_CONST
is not a member ofProblemClass
.
– 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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).
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 aCString
object is created fromMY_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 caseMY_GLOBAL_CONST
contains embedded NUL characters.
– IInspectable
Nov 24 '18 at 18:03
@IInspectable In the context of the original question, how couldMY_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
add a comment |
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).
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 aCString
object is created fromMY_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 caseMY_GLOBAL_CONST
contains embedded NUL characters.
– IInspectable
Nov 24 '18 at 18:03
@IInspectable In the context of the original question, how couldMY_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
add a comment |
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).
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).
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 aCString
object is created fromMY_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 caseMY_GLOBAL_CONST
contains embedded NUL characters.
– IInspectable
Nov 24 '18 at 18:03
@IInspectable In the context of the original question, how couldMY_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
add a comment |
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 aCString
object is created fromMY_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 caseMY_GLOBAL_CONST
contains embedded NUL characters.
– IInspectable
Nov 24 '18 at 18:03
@IInspectable In the context of the original question, how couldMY_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
add a comment |
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
2
In the example,MY_GLOBAL_CONST
is not a member ofProblemClass
.
– 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
add a comment |
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
2
In the example,MY_GLOBAL_CONST
is not a member ofProblemClass
.
– 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
add a comment |
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
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
answered Nov 23 '18 at 16:13
Chris McChris Mc
32225
32225
2
In the example,MY_GLOBAL_CONST
is not a member ofProblemClass
.
– 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
add a comment |
2
In the example,MY_GLOBAL_CONST
is not a member ofProblemClass
.
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
6
Do you have a global variable of type
ProblemClass
orSomeClass
in a source file other thanProblemClass.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 andProblemClass
is only used inSomeClass
– 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 theMY_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
orWinMain
on the call stack, then you are in fact creating an instance ofSomeClass
somewhere as a global variable, or a member thereof.– Igor Tandetnik
Nov 23 '18 at 17:04