C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Static Objects as Struct Members

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
pmatos
Guest





PostPosted: Fri Feb 25, 2005 1:03 pm    Post subject: Static Objects as Struct Members Reply with quote



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

Back to top
Karl Heinz Buchegger
Guest





PostPosted: Fri Feb 25, 2005 1:13 pm    Post subject: Re: Static Objects as Struct Members Reply with quote



pmatos wrote:
Quote:

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.

Quote:
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
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Aslan Kral
Guest





PostPosted: Fri Feb 25, 2005 1:27 pm    Post subject: Re: Static Objects as Struct Members Reply with quote




"pmatos" <pocm (AT) sat (DOT) inesc-id.pt>, haber iletisinde sunlari
yazdi:1109336610.120440.22070 (AT) o13g2000cwo (DOT) googlegroups.com...
Quote:
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*/



Back to top
pmatos
Guest





PostPosted: Fri Feb 25, 2005 2:20 pm    Post subject: Re: Static Objects as Struct Members Reply with quote

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

Back to top
pmatos
Guest





PostPosted: Fri Feb 25, 2005 2:30 pm    Post subject: Re: Static Objects as Struct Members Reply with quote

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.

Back to top
Aslan Kral
Guest





PostPosted: Fri Feb 25, 2005 2:37 pm    Post subject: Re: Static Objects as Struct Members Reply with quote


"pmatos" <pocm (AT) sat (DOT) inesc-id.pt>, haber iletisinde sunlari
yazdi:1109341852.271473.11380 (AT) g14g2000cwa (DOT) googlegroups.com...
Quote:
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.



Back to top
Karl Heinz Buchegger
Guest





PostPosted: Fri Feb 25, 2005 3:15 pm    Post subject: Re: Static Objects as Struct Members Reply with quote

pmatos wrote:
Quote:

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
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Aslan Kral
Guest





PostPosted: Fri Feb 25, 2005 3:38 pm    Post subject: Re: Static Objects as Struct Members Reply with quote


"Karl Heinz Buchegger" <kbuchegg (AT) gascad (DOT) at>, haber iletisinde sunlari
yazdi:421F4100.F5FCDC29 (AT) gascad (DOT) at...
Quote:
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();
}



Back to top
Jay Nabonne
Guest





PostPosted: Sat Feb 26, 2005 6:50 am    Post subject: Re: Static Objects as Struct Members Reply with quote

On Fri, 25 Feb 2005 05:03:30 -0800, pmatos wrote:

Quote:
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


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.