Initializing Static Map Member

Discussion in 'C++' started by utab, May 14, 2006.

  1. utab

    utab Guest

    Dear all, I am confused at some point on the initialization of static
    map member of a class. The class I have designed id something like
    this.

    class Class_name{
    public:

    private:
    .
    .
    void Test_Field(const string &, const string &)throw();
    void Compute_Coordinates(double p)throw(); // p is the
    geometrical parameter
    static map<string,vector<string> > FIELDS; // Initialization
    Outside of Class Declaration
    };

    // Initialization of Static Map Member
    Mesh_Gen::(FIELDS["G"].push_back("G"));
    Mesh_Gen::(FIELDS["G"].push_back("I"));
    Mesh_Gen::(FIELDS["G"].push_back("C"));
    Mesh_Gen::(FIELDS["G"].push_back("X"));

    I tried something like this one but did not compile.

    If you try to initialize a simple static member you can do that with

    type Class_name::variable_name=value // Outside the decleration of the
    class

    Is there a way to that with std::map?

    Regards,
     
    utab, May 14, 2006
    #1
    1. Advertising

  2. utab

    Guest

    utab wrote:
    > Dear all, I am confused at some point on the initialization of static
    > map member of a class. The class I have designed id something like
    > this.
    >
    > class Class_name{
    > public:
    >
    > private:
    > .
    > .
    > void Test_Field(const string &, const string &)throw();
    > void Compute_Coordinates(double p)throw(); // p is the
    > geometrical parameter
    > static map<string,vector<string> > FIELDS; // Initialization
    > Outside of Class Declaration
    > };
    >
    > // Initialization of Static Map Member
    > Mesh_Gen::(FIELDS["G"].push_back("G"));
    > Mesh_Gen::(FIELDS["G"].push_back("I"));
    > Mesh_Gen::(FIELDS["G"].push_back("C"));
    > Mesh_Gen::(FIELDS["G"].push_back("X"));
    >
    > I tried something like this one but did not compile.
    >
    > If you try to initialize a simple static member you can do that with
    >
    > type Class_name::variable_name=value // Outside the decleration of the
    > class
    >
    > Is there a way to that with std::map?



    below is one approach --^-->

    regards
    Andy Little

    #include <map>
    #include <string>
    #include <vector>

    /*
    If you have the boost distro from http://www.boost.org
    use the Boost.Assign library for iniltialising
    multiple values of a vector
    */
    #ifdef HAVE_BOOST
    #include <boost/assign.hpp>
    #endif

    /*
    class_name_map as a singleton
    looks after its own initialisation
    */

    class class_name_map{
    // only for use of Class_name
    friend class Class_name;
    class_name_map()
    {
    if (! this->is_initialised ){
    this->initialise();
    this->is_initialised =true;
    }
    }
    void initialise()
    {
    #ifdef HAVE_BOOST
    using namespace boost::assign;
    this->fields["G"] += "G","I","C","X";
    #else
    this->fields["G"].push_back("G");
    this->fields["G"].push_back("I");
    this->fields["G"].push_back("C");
    this->fields["G"].push_back("X") ;
    #endif
    }
    static std::map< std::string, std::vector< std::string> > fields;
    static bool is_initialised;
    };


    class Class_name : class_name_map {
    public:
    // function added just for testing
    std::vector< std::string> const & operator [] (std::string const &
    in)const
    {
    return this->fields[in];
    }
    };


    // definitions required in a cpp file
    bool class_name_map::is_initialised = false;
    std::map< std::string, std::vector< std::string> >
    class_name_map::fields
    = std::map< std::string, std::vector< std::string> >();

    #include <iostream>
    int main()
    {
    //test it works
    Class_name c;
    for (int i = 0;i < 4;++i){
    std::cout << c["G"] <<'\n';
    }
    }
     
    , May 15, 2006
    #2
    1. Advertising

  3. utab

    Aman Angrish Guest

    "utab" <> wrote in message
    news:

    > Mesh_Gen::(FIELDS["G"].push_back("X"));
    >
    > I tried something like this one but did not compile.


    What's the error you saw ?

    > If you try to initialize a simple static member you can do that with
    >
    > type Class_name::variable_name=value // Outside the decleration of the
    > class
    > Is there a way to that with std::map?


    The map can default initialize. You need to define it outside the
    class.
    map<string, vector<string> > WhatClass::FIELDS;

    regards,
    Aman.





    --
    Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
     
    Aman Angrish, May 15, 2006
    #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. Avi Uziel
    Replies:
    14
    Views:
    807
  2. Steven T. Hatton
    Replies:
    1
    Views:
    6,662
    Siemel Naran
    Apr 19, 2004
  3. Marc D
    Replies:
    2
    Views:
    411
    Marc D
    Feb 14, 2007
  4. dolphin
    Replies:
    3
    Views:
    1,349
    Pete Becker
    Dec 5, 2007
  5. K. Frank
    Replies:
    20
    Views:
    411
Loading...

Share This Page