Simple property access in C++ not working [duplicate]












0















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.










share|improve this 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.




















    0















    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.










    share|improve this 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.


















      0












      0








      0








      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.










      share|improve this question














      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++






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      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.


























          2 Answers
          2






          active

          oldest

          votes


















          2














          Keep in mind that both, std::string s = "hi"; and SomeClass d =
          SomeClass();
          are initialized globally and the order of initialization isn't
          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.






          share|improve this answer























          • 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



















          1














          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.






          share|improve this answer

















          • 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


















          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Keep in mind that both, std::string s = "hi"; and SomeClass d =
          SomeClass();
          are initialized globally and the order of initialization isn't
          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.






          share|improve this answer























          • 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
















          2














          Keep in mind that both, std::string s = "hi"; and SomeClass d =
          SomeClass();
          are initialized globally and the order of initialization isn't
          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.






          share|improve this answer























          • 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














          2












          2








          2






          Keep in mind that both, std::string s = "hi"; and SomeClass d =
          SomeClass();
          are initialized globally and the order of initialization isn't
          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.






          share|improve this answer














          Keep in mind that both, std::string s = "hi"; and SomeClass d =
          SomeClass();
          are initialized globally and the order of initialization isn't
          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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


















          • 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













          1














          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.






          share|improve this answer

















          • 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














          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.






          share|improve this answer

















          • 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








          1






          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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














          • 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



          Popular posts from this blog

          Paul Cézanne

          UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

          Angular material date-picker (MatDatepicker) auto completes the date on focus out