Simple property access in C++ not working [duplicate]
This question already has an answer here:
C++ Global variables and initialization order
1 answer
Each of these files should be in the same directory, and can be copied verbatim. I'm having a very odd property access issue which I don't understand with what I currently know about C++.
main.cpp
#include "someclass.hpp"
SomeClass d = SomeClass();
int main(int argc, const char * argv)
{
SomeClass c = SomeClass();
return 0;
}
someclass.hpp
#ifndef someclass_hpp
#define someclass_hpp
class SomeClass
{
public:
SomeClass();
};
#endif /* someclass_hpp */
someclass.cpp
#include "someclass.hpp"
#include <iostream>
std::string s = "hi";
SomeClass::SomeClass()
{
std::cout << """ + s + """ << std::endl;
}
console
$ g++ main.cpp someclass.hpp someclass.cpp
$ ./a.out
""
"hi"
I've taken a fairly large project and removed everything until I'm left with this very simple bug, which I've been staring at for an hour and can't wrap my head around. Is this a simple problem that I'm not thinking about the right way? As far as I can tell, the code should output "hi" twice, what makes the context of the initialization of a any different from b?
I currently have the solution down to "don't init variables outside of methods", but I'm still curious why this occurs.
c++
marked as duplicate by eyllanesc, Community♦ Nov 20 at 15:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
C++ Global variables and initialization order
1 answer
Each of these files should be in the same directory, and can be copied verbatim. I'm having a very odd property access issue which I don't understand with what I currently know about C++.
main.cpp
#include "someclass.hpp"
SomeClass d = SomeClass();
int main(int argc, const char * argv)
{
SomeClass c = SomeClass();
return 0;
}
someclass.hpp
#ifndef someclass_hpp
#define someclass_hpp
class SomeClass
{
public:
SomeClass();
};
#endif /* someclass_hpp */
someclass.cpp
#include "someclass.hpp"
#include <iostream>
std::string s = "hi";
SomeClass::SomeClass()
{
std::cout << """ + s + """ << std::endl;
}
console
$ g++ main.cpp someclass.hpp someclass.cpp
$ ./a.out
""
"hi"
I've taken a fairly large project and removed everything until I'm left with this very simple bug, which I've been staring at for an hour and can't wrap my head around. Is this a simple problem that I'm not thinking about the right way? As far as I can tell, the code should output "hi" twice, what makes the context of the initialization of a any different from b?
I currently have the solution down to "don't init variables outside of methods", but I'm still curious why this occurs.
c++
marked as duplicate by eyllanesc, Community♦ Nov 20 at 15:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
C++ Global variables and initialization order
1 answer
Each of these files should be in the same directory, and can be copied verbatim. I'm having a very odd property access issue which I don't understand with what I currently know about C++.
main.cpp
#include "someclass.hpp"
SomeClass d = SomeClass();
int main(int argc, const char * argv)
{
SomeClass c = SomeClass();
return 0;
}
someclass.hpp
#ifndef someclass_hpp
#define someclass_hpp
class SomeClass
{
public:
SomeClass();
};
#endif /* someclass_hpp */
someclass.cpp
#include "someclass.hpp"
#include <iostream>
std::string s = "hi";
SomeClass::SomeClass()
{
std::cout << """ + s + """ << std::endl;
}
console
$ g++ main.cpp someclass.hpp someclass.cpp
$ ./a.out
""
"hi"
I've taken a fairly large project and removed everything until I'm left with this very simple bug, which I've been staring at for an hour and can't wrap my head around. Is this a simple problem that I'm not thinking about the right way? As far as I can tell, the code should output "hi" twice, what makes the context of the initialization of a any different from b?
I currently have the solution down to "don't init variables outside of methods", but I'm still curious why this occurs.
c++
This question already has an answer here:
C++ Global variables and initialization order
1 answer
Each of these files should be in the same directory, and can be copied verbatim. I'm having a very odd property access issue which I don't understand with what I currently know about C++.
main.cpp
#include "someclass.hpp"
SomeClass d = SomeClass();
int main(int argc, const char * argv)
{
SomeClass c = SomeClass();
return 0;
}
someclass.hpp
#ifndef someclass_hpp
#define someclass_hpp
class SomeClass
{
public:
SomeClass();
};
#endif /* someclass_hpp */
someclass.cpp
#include "someclass.hpp"
#include <iostream>
std::string s = "hi";
SomeClass::SomeClass()
{
std::cout << """ + s + """ << std::endl;
}
console
$ g++ main.cpp someclass.hpp someclass.cpp
$ ./a.out
""
"hi"
I've taken a fairly large project and removed everything until I'm left with this very simple bug, which I've been staring at for an hour and can't wrap my head around. Is this a simple problem that I'm not thinking about the right way? As far as I can tell, the code should output "hi" twice, what makes the context of the initialization of a any different from b?
I currently have the solution down to "don't init variables outside of methods", but I'm still curious why this occurs.
This question already has an answer here:
C++ Global variables and initialization order
1 answer
c++
c++
asked Nov 20 at 3:51
Makiah
263
263
marked as duplicate by eyllanesc, Community♦ Nov 20 at 15:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by eyllanesc, Community♦ Nov 20 at 15:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Keep in mind that both, std::string s = "hi"; and SomeClass d = are initialized globally and the order of initialization isn't
SomeClass();
guaranteed. This can also lead to undefined behavior.
FYI: your example is crashed on the latest Fedora.
UPD As @davidbak correctly mentioned, my answer is about the order of global initialization in different compilation units.
Global variable initialization has no order between compilation units. In one compilation unit the order is well defined (textual order).
– davidbak
Nov 20 at 4:34
@davidbak, you are right. That's what I meant in my answer. BTW I've updated the answer and clarify it a little bit.
– dshil
Nov 20 at 4:43
add a comment |
Global variable initialization has no guarantee of order. Since the std::string s = "hi"; and SomeClass d = SomeClass(); are both in global scope, when your SomeClass d is created, string s is not initialized, hence the "" output. As dshil correctly pointed out, this would result undefined behaviour and should be avoided.
1
Global variable initialization has no order between compilation units. In one compilation unit the order is well defined (textual order).
– davidbak
Nov 20 at 4:34
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Keep in mind that both, std::string s = "hi"; and SomeClass d = are initialized globally and the order of initialization isn't
SomeClass();
guaranteed. This can also lead to undefined behavior.
FYI: your example is crashed on the latest Fedora.
UPD As @davidbak correctly mentioned, my answer is about the order of global initialization in different compilation units.
Global variable initialization has no order between compilation units. In one compilation unit the order is well defined (textual order).
– davidbak
Nov 20 at 4:34
@davidbak, you are right. That's what I meant in my answer. BTW I've updated the answer and clarify it a little bit.
– dshil
Nov 20 at 4:43
add a comment |
Keep in mind that both, std::string s = "hi"; and SomeClass d = are initialized globally and the order of initialization isn't
SomeClass();
guaranteed. This can also lead to undefined behavior.
FYI: your example is crashed on the latest Fedora.
UPD As @davidbak correctly mentioned, my answer is about the order of global initialization in different compilation units.
Global variable initialization has no order between compilation units. In one compilation unit the order is well defined (textual order).
– davidbak
Nov 20 at 4:34
@davidbak, you are right. That's what I meant in my answer. BTW I've updated the answer and clarify it a little bit.
– dshil
Nov 20 at 4:43
add a comment |
Keep in mind that both, std::string s = "hi"; and SomeClass d = are initialized globally and the order of initialization isn't
SomeClass();
guaranteed. This can also lead to undefined behavior.
FYI: your example is crashed on the latest Fedora.
UPD As @davidbak correctly mentioned, my answer is about the order of global initialization in different compilation units.
Keep in mind that both, std::string s = "hi"; and SomeClass d = are initialized globally and the order of initialization isn't
SomeClass();
guaranteed. This can also lead to undefined behavior.
FYI: your example is crashed on the latest Fedora.
UPD As @davidbak correctly mentioned, my answer is about the order of global initialization in different compilation units.
edited Nov 20 at 4:40
answered Nov 20 at 4:19
dshil
276110
276110
Global variable initialization has no order between compilation units. In one compilation unit the order is well defined (textual order).
– davidbak
Nov 20 at 4:34
@davidbak, you are right. That's what I meant in my answer. BTW I've updated the answer and clarify it a little bit.
– dshil
Nov 20 at 4:43
add a comment |
Global variable initialization has no order between compilation units. In one compilation unit the order is well defined (textual order).
– davidbak
Nov 20 at 4:34
@davidbak, you are right. That's what I meant in my answer. BTW I've updated the answer and clarify it a little bit.
– dshil
Nov 20 at 4:43
Global variable initialization has no order between compilation units. In one compilation unit the order is well defined (textual order).
– davidbak
Nov 20 at 4:34
Global variable initialization has no order between compilation units. In one compilation unit the order is well defined (textual order).
– davidbak
Nov 20 at 4:34
@davidbak, you are right. That's what I meant in my answer. BTW I've updated the answer and clarify it a little bit.
– dshil
Nov 20 at 4:43
@davidbak, you are right. That's what I meant in my answer. BTW I've updated the answer and clarify it a little bit.
– dshil
Nov 20 at 4:43
add a comment |
Global variable initialization has no guarantee of order. Since the std::string s = "hi"; and SomeClass d = SomeClass(); are both in global scope, when your SomeClass d is created, string s is not initialized, hence the "" output. As dshil correctly pointed out, this would result undefined behaviour and should be avoided.
1
Global variable initialization has no order between compilation units. In one compilation unit the order is well defined (textual order).
– davidbak
Nov 20 at 4:34
add a comment |
Global variable initialization has no guarantee of order. Since the std::string s = "hi"; and SomeClass d = SomeClass(); are both in global scope, when your SomeClass d is created, string s is not initialized, hence the "" output. As dshil correctly pointed out, this would result undefined behaviour and should be avoided.
1
Global variable initialization has no order between compilation units. In one compilation unit the order is well defined (textual order).
– davidbak
Nov 20 at 4:34
add a comment |
Global variable initialization has no guarantee of order. Since the std::string s = "hi"; and SomeClass d = SomeClass(); are both in global scope, when your SomeClass d is created, string s is not initialized, hence the "" output. As dshil correctly pointed out, this would result undefined behaviour and should be avoided.
Global variable initialization has no guarantee of order. Since the std::string s = "hi"; and SomeClass d = SomeClass(); are both in global scope, when your SomeClass d is created, string s is not initialized, hence the "" output. As dshil correctly pointed out, this would result undefined behaviour and should be avoided.
answered Nov 20 at 4:29
Sampath
60111024
60111024
1
Global variable initialization has no order between compilation units. In one compilation unit the order is well defined (textual order).
– davidbak
Nov 20 at 4:34
add a comment |
1
Global variable initialization has no order between compilation units. In one compilation unit the order is well defined (textual order).
– davidbak
Nov 20 at 4:34
1
1
Global variable initialization has no order between compilation units. In one compilation unit the order is well defined (textual order).
– davidbak
Nov 20 at 4:34
Global variable initialization has no order between compilation units. In one compilation unit the order is well defined (textual order).
– davidbak
Nov 20 at 4:34
add a comment |