473,605 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is it no possible to fill a static map outside of a block

Dear all,

in a class definition

class X{

private:
static map< string , map<string, int word_map;
static void initialize();
};

and in the implementation file, I may fill out the map inside the
function block not outside any function block. I asked this question
before and got a reply advicing to put those lines in an initializer
function. This works fine but I wondered why I could not define and
initiliaze outside the function block. As

word_map["try"].insert(make_pa ir("try",1));

and the entries outside any of the functions. This is sth related to
static but could not figure that out.

thanks

Dec 19 '06 #1
11 1714
I forgot to write I type

map< string , map<string, int word_map;

first.

utab schreef:
Dear all,

in a class definition

class X{

private:
static map< string , map<string, int word_map;
static void initialize();
};

and in the implementation file, I may fill out the map inside the
function block not outside any function block. I asked this question
before and got a reply advicing to put those lines in an initializer
function. This works fine but I wondered why I could not define and
initiliaze outside the function block. As

word_map["try"].insert(make_pa ir("try",1));

and the entries outside any of the functions. This is sth related to
static but could not figure that out.

thanks
Dec 19 '06 #2
I forgot to write I type

map< string , map<string, int word_map;

first.

utab schreef:
Dear all,

in a class definition

class X{

private:
static map< string , map<string, int word_map;
static void initialize();
};

and in the implementation file, I may fill out the map inside the
function block not outside any function block. I asked this question
before and got a reply advicing to put those lines in an initializer
function. This works fine but I wondered why I could not define and
initiliaze outside the function block. As

word_map["try"].insert(make_pa ir("try",1));

and the entries outside any of the functions. This is sth related to
static but could not figure that out.

thanks
Dec 19 '06 #3
On Dec 19, 4:52 pm, "utab" <umut.ta...@gma il.comwrote:
Dear all,

in a class definition

class X{

private:
static map< string , map<string, int word_map;
static void initialize();

};

and in the implementation file, I may fill out the map inside the
function block not outside any function block. I asked this question
before and got a reply advicing to put those lines in an initializer
function. This works fine but I wondered why I could not define and
initiliaze outside the function block. As

word_map["try"].insert(make_pa ir("try",1));

and the entries outside any of the functions. This is sth related to
static but could not figure that out.
Code outside a function-block is never executed, it's just information
about what should be known when the application starts. But the map
does not exist before then.

--
Erik Wikström

Dec 19 '06 #4
Thanks, but still not clear what is exactly "know".

It is already empty so that I would like to fill that out.
Code outside a function-block is never executed, it's just information
But if I give a integral type I can do

int X::value=3;

Right, so this means that this is executed to associate the memory
location of value to 3
There is a difference in map implementation I guess, but my knowledge
is not that sharp.
about what should be known when the application starts. But the map
does not exist before then.

--
Erik Wikström
Dec 19 '06 #5
Thanks, but still not clear what is exactly "know".

It is already empty so that I would like to fill that out.
Code outside a function-block is never executed, it's just information
But if I give a integral type I can do

int X::value=3;

Right, so this means that this is executed to associate the memory
location of value to 3
There is a difference in map implementation I guess, but my knowledge
is not that sharp.
about what should be known when the application starts. But the map
does not exist before then.

--
Erik Wikström
Dec 19 '06 #6

"utab" <um********@gma il.comwrote in message
news:11******** *************@n 67g2000cwd.goog legroups.com...
Thanks, but still not clear what is exactly "know".

It is already empty so that I would like to fill that out.
Code outside a function-block is never executed, it's just information
But if I give a integral type I can do

int X::value=3;

............... ..............
yes, but you can't say

int X::value = foo();

because foo is an executable statement ( a fucntion call ). the =3 is
actually initializing the variable.

You could say that inside a function block, but not outside.
............... .............

Right, so this means that this is executed to associate the memory
location of value to 3
There is a difference in map implementation I guess, but my knowledge
is not that sharp.
about what should be known when the application starts. But the map
does not exist before then.

--
Erik Wikström

Dec 19 '06 #7

utab wrote:
Code outside a function-block is never executed, it's just information

But if I give a integral type I can do

int X::value=3;

Right, so this means that this is executed to associate the memory
location of value to 3
no, it is compiler generated, not run-time executed
There is a difference in map implementation I guess, but my knowledge
is not that sharp.
the only way you could do what you want would be to something like this

class my_static_map
{
public:

my_static_map()
{
map_["try"].insert(std::ma ke_pair("try", 1));
// ... etc ...
}

typedef std::map<std::s tring, std::map<std::s tring, int map_t;

// pass-through all operations you want to to use from std::map
// interface

map_t::mapped_t ype& operator[](const map_t::key_type & key)
{
return map_[key];
}

map_t::iterator insert(const map_t::value_ty pe & val)
{
return map_.insert(val );
}

void erase(map_t::it erator w)
{
map_.erase(w);
}

// ... etc ...

private:

map_t map_;
};

// .h
class X
{
private:
static my_static_map word_map;
};

// .cpp
X::word_map;

now this is run-time executed initialization

Dec 19 '06 #8
On Tue, 19 Dec 2006 08:40:27 -0800 in comp.lang.c++, "Jim Langston"
<ta*******@rock etmail.comwrote ,
>yes, but you can't say

int X::value = foo();

because foo is an executable statement ( a fucntion call ). the =3 is
actually initializing the variable.

You could say that inside a function block, but not outside.
No, actually you can do exactly that. Complete program,
compiles and runs just fine, follows:

#include <iostream>
int foo()
{
std::cout << "Initialization ...\n";
return 42;
}

struct X
{
static int value;
};

int X::value = foo();

int main()
{
std::cout << X::value << '\n';
}

Dec 19 '06 #9
On 19 Dec 2006 07:52:53 -0800 in comp.lang.c++, "utab"
<um********@gma il.comwrote,
>but I wondered why I could not define and
initiliaze outside the function block. As

word_map["try"].insert(make_pa ir("try",1));

and the entries outside any of the functions.
The above has the form of an executable statement. You can't have any
executable statements outside of a block; only declarations.

The thing that makes it difficult is that you cannot write a constant of
type std::map<anythi ngdirectly (unlike simple arrays of simple types.)
The compiler doesn't know how to build it. You have to take a trip
through the std::map constructor for that to happen.

If you wished, you could write the definition of your static as:

map< string , map<string, int X::word_map = X::initial_map( );

where X::initial_map( ) was a static function that returns a big honking
value of the matching std::map<type, that gets copied to X::word_map.
This would be doing a lot of extra copying during initialization time,
so it's probably not the best choice. I mention it only for the sake of
comparison.

I guess the best choice for you might be to use a "singleton" pattern.
To do that, make X::word_map() a function that returns a reference to
the static map, and call it wherever you access the map. The first time
through, X::word_map() does all the work of building the map; thereafter
it just returns a reference to the existing map. And of course, it can
use all the executable statements it needs to do that, even reading
values from a file or whatever.

By the way, I cannot guess any reason why the value_type of your map is
another map, so I think you are probably making things a heck of a lot
more complicated than you really need to by doing that.

Dec 19 '06 #10

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

Similar topics

6
3146
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope function-prototype scope I think(might be wrong):
34
2147
by: FMorales | last post by:
-- I didn't notice anything about this specifically -- in the c.l.c. FAQ, nor could i get a strait answere -- from my copy of the C standard. Hopeing someone -- here could shed some light on it :)... 6.2.1 "If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope, which terminates at the
3
3921
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /* in above case where is variable k and j mapped in memory layout ? */
4
1571
by: Ant | last post by:
Hi I'm quite new to C#. I've been playing around with variables declared outside the scope of a method with & without the static keyword. What difference does declaring variables in either fashion make? For that matter, why must you declare methods with the static keyword? Thanks for any ideas Ant
11
3814
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 ways to do this through prototyping and other techniques, but these behaviors need to be static and...
18
2532
by: Ronald Bruck | last post by:
I have several routines which are used millions of times in my programs, using the Gnu multi-precision software's floating-point reals (see <http://www.swox.se/gmp>). These are of type mpf_t, and I must explicitly initialize (and later free) all temporary variables of this type. So I hit on the idea of something like this: static int firstcall = 1; mpf_t temp;
18
33867
by: Jack | last post by:
Thanks.
55
6186
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
25
120623
by: shadyabhi | last post by:
I heard that all global variables are static.. If its so then what is the difference between the 2 declarations: #include<stdio.h> int a; static int b; main()
0
7934
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8424
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8415
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8069
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8286
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6742
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5886
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2438
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
1
1537
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.