Static Objects as Struct Members

P

pmatos

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
 
K

Karl Heinz Buchegger

pmatos said:
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.
 
A

Aslan Kral

haber iletisinde sunlari said:
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*/
 
P

pmatos

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
 
P

pmatos

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

Aslan Kral

haber iletisinde sunlari said:
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.
 
K

Karl Heinz Buchegger

pmatos said:
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.
 
A

Aslan Kral

haber iletisinde sunlari said:
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();
}
 
J

Jay Nabonne

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?

How about:

// Example class
class MyClass
{
static std::map<int, int> rules;
};

// Create an array of values.
static std::pair<int, int> ruleValues[] =
{
std::pair<int, int>(-1, 1),
std::pair<int, int>(-2, 2),
std::pair<int, int>(-3, 3)
};

// Pass the start and end+1 of the array
std::map<int, int> MyClass::rules(ruleValues,
ruleValues+sizeof(ruleValues)/sizeof(ruleValues[0]));

- Jay
 

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
474,285
Messages
2,571,416
Members
48,107
Latest member
AmeliaAmad

Latest Threads

Top