473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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_pair("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 1695
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_pair("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_pair("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...@gmail.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_pair("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********@gmail.comwrote in message
news:11*********************@n67g2000cwd.googlegro ups.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::make_pair("try", 1));
// ... etc ...
}

typedef std::map<std::string, std::map<std::string, int map_t;

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

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

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

void erase(map_t::iterator 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*******@rocketmail.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********@gmail.comwrote,
>but I wondered why I could not define and
initiliaze outside the function block. As

word_map["try"].insert(make_pair("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<anythingdirectly (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
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.
It is more clear now.
Actually I have a structure like the one below

lets say, f stands for fields(strings) and each row is determined by f1
so that it represents another field. But in the mean time I would like
a structure like (since f1 determines the properties)

f1 - <string , int>

fid :1 2 3 4 5 6 7 8

f1 f2 f3 f4 f5 f6 f7 f8

fid:field id

You are right this was a class that I designed last year but now I see
a lot of flaws in that. So it has to be designed by more attention.

Dec 19 '06 #11
On 19 Dec 2006 10:41:45 -0800 in comp.lang.c++, "utab"
<um********@gmail.comwrote,
>lets say, f stands for fields(strings) and each row is determined by f1
so that it represents another field. But in the mean time I would like
a structure like (since f1 determines the properties)

f1 - <string , int>
That is getting simple enough that the compiler can do a greater part of
the work at compile time (however, still leaving plenty for run time, so
maybe not efficient enough for you. But convenient.)
Using the "two iterators" std::map constructor:
#include <iostream>
#include <map>
#include <string>
using namespace std;

typedef std::pair<std::string, intinit;
const init init_map[] = {
init("f1", 1), init("f2", 2), init("f3", 3)
};
const int init_count = sizeof(init_map)/sizeof(init_map[0]);

std::map<string,intword_map(init_map, init_map+init_count);

int main()
{
std::cout << word_map["f2"] << '\n';
}

Dec 19 '06 #12

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

Similar topics

6
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...
34
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...
3
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; } /*...
4
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...
11
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...
18
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...
18
by: Jack | last post by:
Thanks.
55
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...
25
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
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...

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.