473,569 Members | 2,791 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 1600
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_KeyFromVa lue( int Value )
{
return -Value;
}

given a key (-1 .. -3), what is the correct value for it:
int Rules_ValueFrom Key( 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.ine sc-id.pt>, haber iletisinde sunlari
yazdi:11******* **************@ o13g2000cwo.goo glegroups.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.ine sc-id.pt>, haber iletisinde sunlari
yazdi:11******* **************@ g14g2000cwa.goo glegroups.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_dumm y = InitRules();

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

return true;
}

Again: The variable rules_init_dumm y 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******@gasca d.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
1800
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
1701
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
1660
by: Alex Vinokur | last post by:
I have the following problem. class Base { public: void (*pfunc) (int) = 0; }; class Derived : public Base {
7
11991
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
3810
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of...
8
2828
by: Markus Henschel | last post by:
Hello, this is a test case of something I just can't explain myself: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include <list> typedef void (*registerfunc)(); class CMgr {
4
2557
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 error struct xxxx { static int i; // int j; };
8
8914
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. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
2
1960
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.) ___TYPE___ typedef struct Data_s { char* Name_p;
0
7605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
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. ...
0
8118
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...
1
7665
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...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
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...
0
3651
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...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
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

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.