how to initialize a class static member variable of composite type

Discussion in 'C++' started by Nan Li, Nov 9, 2005.

  1. Nan Li

    Nan Li Guest

    Hello,
    I got stuck in a situation I think many of you must have already
    experienced. I want to have a simple lookup map inside my class. All
    the instances share that map. The code is as follows.

    class A
    {
    private:
    static std::map<int,std::string> someMap;
    public:
    ......
    };

    But I don't know how I can initialize this map properly.


    Thanks,
    Nan
    Nan Li, Nov 9, 2005
    #1
    1. Advertising

  2. You just need a start condition and verify it in the construction of
    every instance of class A:

    A::A()
    {
    if( ! someMapStarted )
    {
    startSomeMap();
    someMapStarted = true;
    }
    }

    Needles to say that this variable need to be static =)
    Wanderley Caloni, Nov 9, 2005
    #2
    1. Advertising

  3. Re: how to initialize a class static member variable of compositetype

    Nan Li wrote:
    > Hello,
    > I got stuck in a situation I think many of you must have already
    > experienced. I want to have a simple lookup map inside my class. All
    > the instances share that map. The code is as follows.
    >
    > class A
    > {
    > private:
    > static std::map<int,std::string> someMap;
    > public:
    > ......
    > };
    >
    > But I don't know how I can initialize this map properly.


    What would you like to have in that map? There are several constructors
    in the 'map' template. One takes the comparator and the allocator (and it
    is also the default constructor), another takes two iterators, yet another
    is a copy-constructor. Do you want some information to be put into that
    shared map at the time of construction?

    std::map<int,std::string> A::someMap; // default-initialisation

    std::map<int,std::string> A::someMap(iter1,iter2); // initialisation
    // from 2 iterators

    (to use that the iterators need to dereference to a type that can be
    inserted into your map, namely, std::pair<int,std::string>).

    V
    Victor Bazarov, Nov 9, 2005
    #3
  4. Nan Li

    Neil Cerutti Guest

    On 2005-11-09, Nan Li <> wrote:
    > Hello,
    >
    > I got stuck in a situation I think many of you must have
    > already experienced. I want to have a simple lookup map inside
    > my class. All the instances share that map. The code is as
    > follows.
    >
    > class A
    > {
    > private:
    > static std::map<int,std::string> someMap;
    > public:
    > ......
    > };
    >
    > But I don't know how I can initialize this map properly.


    Here's one way. Put the map inside a static function.

    class A
    {
    static const std::map<int, std::string>& get_map()
    {
    static std::map<int, std::string> the_map;
    if (the_map.size() == 0) {
    /* Initialize the_map */
    }
    return the_map;
    }
    /* ... */
    }

    If it's possible for an initialized map to have size 0, then
    you'll need to use a flag instead.

    --
    Neil Cerutti
    Neil Cerutti, Nov 9, 2005
    #4
  5. Nan Li

    Nan Li Guest

    Victor Bazarov wrote:
    > Nan Li wrote:
    > > Hello,
    > > I got stuck in a situation I think many of you must have already
    > > experienced. I want to have a simple lookup map inside my class. All
    > > the instances share that map. The code is as follows.
    > >
    > > class A
    > > {
    > > private:
    > > static std::map<int,std::string> someMap;
    > > public:
    > > ......
    > > };
    > >
    > > But I don't know how I can initialize this map properly.

    >
    > What would you like to have in that map? There are several constructors
    > in the 'map' template. One takes the comparator and the allocator (and it
    > is also the default constructor), another takes two iterators, yet another
    > is a copy-constructor. Do you want some information to be put into that
    > shared map at the time of construction?
    >
    > std::map<int,std::string> A::someMap; // default-initialisation
    >
    > std::map<int,std::string> A::someMap(iter1,iter2); // initialisation
    > // from 2 iterators
    >
    > (to use that the iterators need to dereference to a type that can be
    > inserted into your map, namely, std::pair<int,std::string>).
    >
    > V


    Thanks a lot. Following your advice, I came up with the following code
    and it works fine.

    using namespace std;


    typedef pair<int,string> IntStringPair;


    class A
    {
    public:
    static const IntStringPair MAPPINGS[];
    static const map<int, string> someMap;
    };


    const IntStringPair A::MAPPINGS[] = { IntStringPair(1, "foo" ),
    IntStringPair(2, "bar")
    };


    const std::map<int, string> A::someMap( MAPPINGS,
    MAPPINGS + sizeof( MAPPINGS ) /
    sizeof( MAPPINGS[0] ) );
    Nan Li, Nov 9, 2005
    #5
  6. Re: how to initialize a class static member variable of compositetype

    Nan Li wrote:
    > Hello,
    > I got stuck in a situation I think many of you must have already
    > experienced. I want to have a simple lookup map inside my class. All
    > the instances share that map. The code is as follows.
    >
    > class A
    > {
    > private:
    > static std::map<int,std::string> someMap;
    > public:
    > ......
    > };
    >
    > But I don't know how I can initialize this map properly.
    >
    >
    > Thanks,
    > Nan
    >


    If you don't mind the one-off cost of copying a map there's nothing
    wrong with the obvious

    class A
    {
    static std::map<int, std::string> someMap;
    };

    ....

    std::map<int, std::string> A::someMap = some_function();

    Whatever some_function returns will initialise SomeMap.

    john
    John Harrison, Nov 9, 2005
    #6
  7. Nan Li

    Nan Li Guest

    John Harrison wrote:
    > Nan Li wrote:
    > > Hello,
    > > I got stuck in a situation I think many of you must have already
    > > experienced. I want to have a simple lookup map inside my class. All
    > > the instances share that map. The code is as follows.
    > >
    > > class A
    > > {
    > > private:
    > > static std::map<int,std::string> someMap;
    > > public:
    > > ......
    > > };
    > >
    > > But I don't know how I can initialize this map properly.
    > >
    > >
    > > Thanks,
    > > Nan
    > >

    >
    > If you don't mind the one-off cost of copying a map there's nothing
    > wrong with the obvious
    >
    > class A
    > {
    > static std::map<int, std::string> someMap;
    > };
    >
    > ...
    >
    > std::map<int, std::string> A::someMap = some_function();
    >
    > Whatever some_function returns will initialise SomeMap.
    >
    > john


    This is a good idea. I can do something like below. The actual map is
    inside the function( see Neil's post )

    class A
    {
    private:
    static const map<int, string>& someMap;
    static const map<int,string>& initSomeMap();
    };


    const map<int,string>& A::initSomeMap()
    {
    static map<int, string> m;
    m[1] = "foo";
    m[2] = "bar";
    return m;
    };

    const map<int, string>& A::someMap = A::initSomeMap() ;
    Nan Li, Nov 9, 2005
    #7
  8. Nan Li

    Nan Li Guest

    Wanderley Caloni wrote:
    > You just need a start condition and verify it in the construction of
    > every instance of class A:
    >
    > A::A()
    > {
    > if( ! someMapStarted )
    > {
    > startSomeMap();
    > someMapStarted = true;
    > }
    > }
    >
    > Needles to say that this variable need to be static =)


    Thanks. But in this particular case, I actually don't want to do lazy
    loading, and I don't want to have a check every time an object is
    created.
    Nan Li, Nov 9, 2005
    #8
    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. Sam
    Replies:
    4
    Views:
    459
    The Directive
    Jan 13, 2004
  2. Siemel Naran
    Replies:
    4
    Views:
    785
    Micah Cowan
    Jan 12, 2005
  3. Jim Langston
    Replies:
    5
    Views:
    354
    Jim Langston
    Sep 23, 2005
  4. markww
    Replies:
    6
    Views:
    5,132
    Salt_Peter
    Aug 26, 2006
  5. subramanian
    Replies:
    9
    Views:
    415
    Salt_Peter
    Jan 3, 2007
Loading...

Share This Page