473,671 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initializing Static Map Member

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:
May 14 '06 #1
2 9278
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(cons t string &, const string &)throw();
void Compute_Coordin ates(double p)throw(); // p is the
geometrical parameter
static map<string,vect or<string> > FIELDS; // Initialization
Outside of Class Declaration
};

// Initialization of Static Map Member
Mesh_Gen::(FIEL DS["G"].push_back("G") );
Mesh_Gen::(FIEL DS["G"].push_back("I") );
Mesh_Gen::(FIEL DS["G"].push_back("C") );
Mesh_Gen::(FIEL DS["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::var iable_name=valu e // 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_initialis ed ){
this->initialise() ;
this->is_initialis ed =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';
}
}

May 15 '06 #2
"utab" <um********@gma il.com> wrote in message
news:11******** *************@g 10g2000cwb.goog legroups.com
Mesh_Gen::(FIEL DS["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::var iable_name=valu e // 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::FIEL DS;

regards,
Aman.

--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
May 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
10635
by: Avi Uziel | last post by:
Hi All, I'm writing a Windows DLL which contain some utility classes. One of my classes is Singleton, therefore contain some static members. I'm using VC6 and linking to the DLL statically. My question is where is the correct (and best way) to initialize theses variables? If I initialize them in the class CPP (in the DLL) then I get linkage error when I try to link my project with the LIB, and I don't want to make the user initialize...
2
2041
by: Neno | last post by:
Hi, I have a linkage error that has something to do with the use of a static member array and I can't understand how to solve the problem. Can someone help me please? In detail: A class Month (it is just an unsafe and useless example) has protected constructors so that Month objects can be obtained only through a static member function named getMonth. Month class has a private static array named months made of 12 pointers to Month...
4
24427
by: Mantorok Redgormor | last post by:
Is this legal? int foo = { 0 }; gcc gives: foo.c: In function `main': foo.c:5: warning: missing braces around initializer foo.c:5: warning: (near initialization for `foo') foo.c:5: warning: unused variable `foo'
0
1107
by: Adam Smith | last post by:
Are there any drawbacks to initializing static member variables for classes used in an asp.net application within application_start? My classes have arraylist like tables, which cache information so that I don't have to hit the database every call. I would like to initialize these tables through calling a static class function, Init(), in application start. Thanks.
17
2608
by: Calle Pettersson | last post by:
Coming from writing mostly in Java, I have trouble understanding how to declare a member without initializing it, and do that later... In Java, I would write something like public static void main(String args) { MyType aMember; ... aMember = new MyType(...) ... } However, in C++ this does not seem to work. I declare in class (it's
14
5922
by: Madhav | last post by:
hi all, I was trying the following code on the MS .net compiler: class Item { static int count; public: //static void init() { } Item()
8
2767
by: John | last post by:
Hello, is there any compiler option for g++ for initializing static members of the class. Due to some unknown reason, static member in one of our c++ application is not getting initialized properly. Please help me on this. Thanks,
6
3239
by: Grey Alien | last post by:
class A { public: A(const B& ref); private: static B& b ; }; How may b be initialized ?
3
2820
by: Ramesh | last post by:
Hi, I am trying to create an array of pointers to member functions inside my class. When I created a global array of type pfn & initialized with member functions and copy it back to the member function pointer array (Handlers) - compiler doesnt let me do that. I got an error as below: "invalid use of non-static member function bool test::func1(long int)"
0
8917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8821
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8598
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4407
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2812
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1809
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.