Initializing Static Map Member

U

utab

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,
 
A

andy

utab said:
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';
}
}
 
A

Aman Angrish

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top