initialization sequence in header file

Discussion in 'C++' started by zl2k, Apr 6, 2012.

  1. zl2k

    zl2k Guest

    I have a header file like this:

    #include <map>
    #include <set>

    struct SET_CREATOR{
    static std::set<std::string> CreateSet() {
    std::set<std::string> string_set;
    string_set.insert("ab");
    return string_set;
    }
    };
    static std::set<std::string> MY_STRING_SET(SET_CREATOR::CreateSet());

    struct MAP_CREATOR{
    static std::map<std::string, std::string> CreateMap() {
    std::map<std::string, std::string> string_map;
    std::cout<<*MY_STRING_SET.cbegin()<<std::endl; // segmentation
    error here
    return string_map;
    }
    };
    static std::map<std::string, std::string>
    MY_STRING_MAP(MAP_CREATOR::CreateMap());

    My question is: how may I initialize MY_STRING_SET properly so that it
    can be used by other struct in the same header file? Thanks for help.

    zl2k
     
    zl2k, Apr 6, 2012
    #1
    1. Advertising

  2. On 4/6/2012 4:01 PM, zl2k wrote:
    > I have a header file like this:
    >
    > #include<map>
    > #include<set>
    >
    > struct SET_CREATOR{
    > static std::set<std::string> CreateSet() {
    > std::set<std::string> string_set;
    > string_set.insert("ab");
    > return string_set;
    > }
    > };
    > static std::set<std::string> MY_STRING_SET(SET_CREATOR::CreateSet());


    This defines a global variable (that's the intent, yes?) but the
    definition is in the header, which means each module that includes that
    header will have its own 'MY_STRING_SET' symbol, and they are *not*
    related to each other at all.

    >
    > struct MAP_CREATOR{
    > static std::map<std::string, std::string> CreateMap() {
    > std::map<std::string, std::string> string_map;
    > std::cout<<*MY_STRING_SET.cbegin()<<std::endl; // segmentation
    > error here
    > return string_map;
    > }
    > };
    > static std::map<std::string, std::string>
    > MY_STRING_MAP(MAP_CREATOR::CreateMap());
    >
    > My question is: how may I initialize MY_STRING_SET properly so that it
    > can be used by other struct in the same header file? Thanks for help.


    Header files are not compiled. They are text, parts of translation
    units. You shouldn't ever *define* variables in headers. What is it
    you're trying to do? What you're probably running into is "static
    object initialization fiasco", where you're trying to use the static
    object (the set in your case) before it has been properly initialized.

    A way to avoid that is to make sure the objects are initialized in
    proper sequence by calling those function one after the other in another
    initializer. But first sort out the "static" issue and the fact that
    they are *defined* in a header.

    If you're trying to create a singleton (or two singletons with the
    second one aware of the first), there are code patterns for that. Try
    googling "singleton C++".

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 6, 2012
    #2
    1. Advertising

  3. zl2k

    zl2k Guest

    On Apr 6, 3:33 pm, Victor Bazarov <> wrote:
    > On 4/6/2012 4:01 PM, zl2k wrote:
    >
    > > I have a header file like this:

    >
    > > #include<map>
    > > #include<set>

    >
    > > struct SET_CREATOR{
    > >      static std::set<std::string>  CreateSet() {
    > >          std::set<std::string>  string_set;
    > >          string_set.insert("ab");
    > >          return string_set;
    > >      }
    > > };
    > > static std::set<std::string>  MY_STRING_SET(SET_CREATOR::CreateSet());

    >
    > This defines a global variable (that's the intent, yes?) but the
    > definition is in the header, which means each module that includes that
    > header will have its own 'MY_STRING_SET' symbol, and they are *not*
    > related to each other at all.
    >
    >
    >
    > > struct MAP_CREATOR{
    > >      static std::map<std::string, std::string>  CreateMap() {
    > >          std::map<std::string, std::string>  string_map;
    > >          std::cout<<*MY_STRING_SET.cbegin()<<std::endl; // segmentation
    > > error here
    > >          return string_map;
    > >      }
    > > };
    > > static std::map<std::string, std::string>
    > > MY_STRING_MAP(MAP_CREATOR::CreateMap());

    >
    > > My question is: how may I initialize MY_STRING_SET properly so that it
    > > can be used by other struct in the same header file? Thanks for help.

    >
    > Header files are not compiled.  They are text, parts of translation
    > units.  You shouldn't ever *define* variables in headers.  What is it
    > you're trying to do?  What you're probably running into is "static
    > object initialization fiasco", where you're trying to use the static
    > object (the set in your case) before it has been properly initialized.
    >
    > A way to avoid that is to make sure the objects are initialized in
    > proper sequence by calling those function one after the other in another
    > initializer.  But first sort out the "static" issue and the fact that
    > they are *defined* in a header.
    >
    > If you're trying to create a singleton (or two singletons with the
    > second one aware of the first), there are code patterns for that.  Try
    > googling "singleton C++".
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask


    Thank you, it is the "static object initialization fiasco". I use the
    SET_CREATOR::CreateSet() inside of the 2nd struct and the problem is
    solved.
    zl2k
     
    zl2k, Apr 6, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    4
    Views:
    716
    lilburne
    Nov 2, 2003
  2. Cheng Mo
    Replies:
    4
    Views:
    558
    Gianni Mariani
    Dec 2, 2004
  3. stef mientki
    Replies:
    13
    Views:
    647
    stef mientki
    Oct 20, 2007
  4. Christof Warlich

    initialization sequence issue

    Christof Warlich, Jan 5, 2008, in forum: C++
    Replies:
    9
    Views:
    377
    James Kanze
    Jan 7, 2008
  5. mlt
    Replies:
    2
    Views:
    854
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page