473,468 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

static object initialization

Hi,

this is an STL newbe question, sorry if it is too stupid:

Is there a way to statically _initialize_ an STL map, i.e. not at runtime?

There seems to be a map constructor that expects a start and an end
iterator, which
gives me some hope. But then I need something to iteate upon, and as
much as I
understand, this must be an array of STL pairs that match the map's
template
specification. Thus, could my problem be reduced to creating an array of
pairs,
and if yes, how do I statically create _and_ initialize an array of
objects (i.e. pairs)?

Thanks for any help,

Christof

Jul 23 '05 #1
4 1773
Christof Warlich wrote:
this is an STL newbe question, sorry if it is too stupid:

Is there a way to statically _initialize_ an STL map, i.e. not at runtime?


No. In reality only PODs can be initialised at program loading. All
other types have to be constructed and that can only be done at run-time.

V
Jul 23 '05 #2
Christof Warlich wrote:
Is there a way to statically _initialize_ an STL map, i.e. not at runtime?


I should have looked at the FAQ first, sorry
(http://www.parashift.com/c++-faq-lite/ctors.html):

Fred a[10] = {
Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7),
Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7)
};

So here is the solution for the map:

#include <map>
#include <iostream>

pair<const char *, int> a[] =
{
pair<const char *, int>("willi", 3),
pair<const char *, int>("max", 4),
};

map<const char *, int> myMap(a, a + sizeof(a) / sizeof(a[0]));

int main() {
cout << myMap["willi"] << "\n";
cout << myMap["max"] << "\n";
return 0;
}

Anyway, thanks for helping.

Regards,

Christof

Jul 23 '05 #3
Christof Warlich wrote:
Christof Warlich wrote:
Is there a way to statically _initialize_ an STL map, i.e. not at
runtime?

Not at run-time means at compile time. The answer is still "No".
I should have looked at the FAQ first, sorry
(http://www.parashift.com/c++-faq-lite/ctors.html):

Fred a[10] = {
Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7),
Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7), Fred(5,7)
};
If 'Fred' is a non-POD, which is very likely, every object has to still
undergo initialisation by a constructor, which means run-time.
So here is the solution for the map:

#include <map>
#include <iostream>

pair<const char *, int> a[] =
{
pair<const char *, int>("willi", 3),
pair<const char *, int>("max", 4),
};

map<const char *, int> myMap(a, a + sizeof(a) / sizeof(a[0]));

int main() {
cout << myMap["willi"] << "\n";
cout << myMap["max"] << "\n";
return 0;
}


That's a solution for static map initialisation, but it involves run-time
nonetheless. The constructors for 'myMap' elements *will* be invoked b4
'main' is called, but *not* during compile time, mind you.

V
Jul 23 '05 #4
Victor Bazarov wrote:
That's a solution for static map initialisation, but it involves run-time
nonetheless. The constructors for 'myMap' elements *will* be invoked b4
'main' is called, but *not* during compile time, mind you.

V


Hi Victor,

you are right, the wording in my question was misleading. In fact,
I was just only looking for an initialization outside of main.
So I'm happy now.

Thanks again,

Christof
Jul 23 '05 #5

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

Similar topics

1
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
5
by: BigMan | last post by:
Does the standard define the order in which static members of a class are created and initialized?
3
by: Mike - EMAIL IGNORED | last post by:
MyClass { //I have a static member method: void static myMethod(); //and a static data member: static MyType myData; }; //In the .cpp file: void MyClass::myMethod()
7
by: John A Grandy | last post by:
For a singleton class utilizes by ASP.NET 2.0 page processing: When initial instantiation is performed during the initial call to the retrieve instance method (let's call the method...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
6
by: gs | last post by:
Hi, I want to know that when memory get allocated to static data member of a class. class A { static int i; }
17
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
1
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...
0
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...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.