Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > initialization sequence in header file

Reply
Thread Tools

initialization sequence in header file

 
 
zl2k
Guest
Posts: n/a
 
      04-06-2012
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-06-2012
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
 
Reply With Quote
 
 
 
 
zl2k
Guest
Posts: n/a
 
      04-06-2012
On Apr 6, 3:33*pm, Victor Bazarov <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
how to iterate over sequence and non-sequence ? stef mientki Python 13 10-20-2007 10:21 AM
What is better /standard for creating files. a cpp file with header or cpp and seperate file for header DrUg13 C++ 1 02-10-2004 09:20 AM
BOOT SEQUENCE (how to change boot sequence) bird Computer Support 13 12-24-2003 02:20 AM
how to avoid using another header file inside a header file? Newsgroup - Ann C++ 4 11-02-2003 01:20 PM



Advertisments