473,503 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Objects as Struct Members

Hi all,

I have a struct with a:
static map<int, int> rules;

Now, I want it to always be initialized with the pairs (-1, 1), (-2, 2)
and (-3, 3). I don't need to dynamically add or delete pairs. I only
need the struct to have this constant static map. How can I initialize
it?

I've tried some solutions like having in the struct constructor:
rules[-1] = 1;
rules[-2] = 2;
rules[-3] = 3;

But it doesn't work. Can somebody help me with this?

Cheers,

Paulo Matos

Jul 23 '05 #1
7 1597
pmatos wrote:

Hi all,

I have a struct with a:
static map<int, int> rules;

Now, I want it to always be initialized with the pairs (-1, 1), (-2, 2)
and (-3, 3). I don't need to dynamically add or delete pairs. I only
need the struct to have this constant static map.
That's quite unusal. Why would you use a map for this, other then
beeing able to use the access syntax?

Why not simply

struct mapping
{
int key;
int value;
}

static mapping rules[] = { { -1, 1 }, {-2, 2}, {-3, 3} };

But then again, looking at the numbers, the whole rules-mapping
can be replaced by simple formulas:

given the value (1 .. 3), what is the correct key for it:
int Rules_KeyFromValue( int Value )
{
return -Value;
}

given a key (-1 .. -3), what is the correct value for it:
int Rules_ValueFromKey( int Key )
{
return -Key;
}

You could add some error checking (Key in range, Value in range), but
basically this gives the very same functionality then your std::map.
And it is simpler and faster also.
How can I initialize
it?

I've tried some solutions like having in the struct constructor:
rules[-1] = 1;
rules[-2] = 2;
rules[-3] = 3;


Well. That wouldn't work. Mostly because rules is not some struct, but
is a std::map. And the constructor for a std::map is already written
and outside your reach. Just put that code somewhere into a strategic
place (eg. at the very beginning of main() or some function that is
called when your application starts up).

But the question remains: With that requirements why do you use
a std::map at all? There is really no need for it.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2

"pmatos" <po**@sat.inesc-id.pt>, haber iletisinde sunlari
yazdi:11*********************@o13g2000cwo.googlegr oups.com...
Hi all,

I have a struct with a:
static map<int, int> rules;

Now, I want it to always be initialized with the pairs (-1, 1), (-2, 2)
and (-3, 3). I don't need to dynamically add or delete pairs. I only
need the struct to have this constant static map. How can I initialize
it?

I've tried some solutions like having in the struct constructor:
rules[-1] = 1;
rules[-2] = 2;
rules[-3] = 3;

But it doesn't work. Can somebody help me with this?

Cheers,

Paulo Matos


The following does what you want:

struct staticmap : map<int, int>
{
staticmap()
{
(*this)[-1] = 1;
(*this)[-2] = 2;
(*this)[-3] = 3;
}
};
static staticmap staticrules;
static map<int, int>& rules = staticrules; /*use rules as you wish*/
Jul 23 '05 #3
Ok, probably I didn't explain myself correctly. Of course, I don't want
something like that 'literally'! I just asked the question in a way
that you could understand the context and solve my problem at the same
time. The issue is that I have a lot of pairs which should be put in a
map to send to a library function which explicitly waits for a map. And
the pairs are constant, I don't need to change them. They are not -1, 1
and -2,2 ... etc but the issue remains the same.

I want to create a static map and initialize it so that I can send it
to the library function that expects it. If you're just curious, the
library is spirit and the map is a map of parser_id, string which
associates rules identification numbers with strings which are the name
of the rules, the function tree_to_xml expects this map. Since I have a
list of fixed parser rules and a list of names for them I can create a
map that will never be changed dynamically.

Anyway, thanks for the help.

Cheers,

Paulo Matos

Jul 23 '05 #4
That way won't work because rules will not be a static member of my
struct. Even if inside my struct I add your code, I'll have defined
staticrules which will be useless since it was defined only to define
rules.

Jul 23 '05 #5

"pmatos" <po**@sat.inesc-id.pt>, haber iletisinde sunlari
yazdi:11*********************@g14g2000cwa.googlegr oups.com...
That way won't work because rules will not be a static member of my
struct. Even if inside my struct I add your code, I'll have defined
staticrules which will be useless since it was defined only to define
rules.

There is no static member in the code. That answers your question regarding
initializing the map before main() is entered. Of course you can do it some
other ways too.
Jul 23 '05 #6
pmatos wrote:

That way won't work because rules will not be a static member of my
struct. Even if inside my struct I add your code, I'll have defined
staticrules which will be useless since it was defined only to define
rules.


I really don't understand that argumentation.
What Aslan suggested was to encapsulate that map into
another structure, just to get a hands on defining a
constructor. That should work pretty well. In fact
you never ever touch the staticrules object again. After
the constructor is run, it has done its job and can
be forgotten completely.

But if you are unhappy with that, you can always use
another dummy variable and use a function to initialize
that variable. Inside that function you fill your map.

static map<int, int> rules;
static bool rules_init_dummy = InitRules();

static bool InitRules()
{
rules[-1] = 1;
rules[-2] = 2;
rules[-3] = 3;

return true;
}

Again: The variable rules_init_dummy is not important. It is only
there to have a way to call a function (namely 'InitRules') at
program startup time.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #7

"Karl Heinz Buchegger" <kb******@gascad.at>, haber iletisinde sunlari
yazdi:42***************@gascad.at...
pmatos wrote:

That way won't work because rules will not be a static member of my
struct. Even if inside my struct I add your code, I'll have defined
staticrules which will be useless since it was defined only to define
rules.


I really don't understand that argumentation.
What Aslan suggested was to encapsulate that map into
another structure, just to get a hands on defining a
constructor. That should work pretty well. In fact
you never ever touch the staticrules object again. After
the constructor is run, it has done its job and can
be forgotten completely.


In fact staticrules can be avoided like this:

#include <map>
using namespace std;

struct staticmap : map<int, int>
{
staticmap()
{
(*this)[-1] = 1;
(*this)[-2] = 2;
(*this)[-3] = 3;
}
};
static map<int, int>& rules = staticmap();

int main()
{
size_t size = rules.size();
}
Jul 23 '05 #8

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

Similar topics

5
1795
by: Luther Baker | last post by:
Hi, Is the order of initialization guaranteed for static members as it is for instance members? Namely, the order they appear the in the declaration? ie: foo.h:
3
1699
by: SteveM | last post by:
This is probably an easy answer but since I am fairly new to C++ for some reason I can't see it :-( Any help would be appreciated :-) I have a class: class AClass { public:
13
1655
by: Alex Vinokur | last post by:
I have the following problem. class Base { public: void (*pfunc) (int) = 0; }; class Derived : public Base {
7
11986
by: Raxit | last post by:
Can A C Program having static Variable in Structure is possible..... i.e. struct A { int a; static int b; };
11
3795
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
2825
by: Markus Henschel | last post by:
Hello, this is a test case of something I just can't explain myself: ...
4
2542
by: sandeep | last post by:
Hi why we cannot have static as a structure member? & also is there any way to achive data hiding in C at this level( i.e. access only selected structure member ) following code gives syntax...
8
8910
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
2
1954
by: Andreas Lundgren | last post by:
Hi! I want to put some values in static memory, and then be able to reference this data from my code in a decent way by referencing AllData.Data. (All code beneth is placed outside functions.) ...
0
7194
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7267
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,...
0
7316
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...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3160
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...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
372
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...

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.