Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initializing Static Map Member

Reply
Thread Tools

Initializing Static Map Member

 
 
utab
Guest
Posts: n/a
 
      05-14-2006
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,

 
Reply With Quote
 
 
 
 
andy@servocomm.freeserve.co.uk
Guest
Posts: n/a
 
      05-15-2006
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"][i] <<'\n';
}
}

 
Reply With Quote
 
 
 
 
Aman Angrish
Guest
Posts: n/a
 
      05-15-2006
"utab" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com

> 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
 
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
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
initializing static const map Marc D C++ 2 02-14-2007 01:52 AM
linkage error when initializing static member array Neno C++ 2 10-23-2004 10:15 AM
Initializing a static const member array of a class type Steven T. Hatton C++ 1 04-19-2004 06:42 AM
Initializing static class member Avi Uziel C++ 14 09-26-2003 06:53 PM



Advertisments