initialize stl map in static scope
-
for primitive data type, i am able to initialize them in static scope: class x { public: static int var[]; }; /* We are able to have var initialize before x object being instance. */ int x::var[] = {1, 2, 3}; However, for a stl map, how i can have elements inserted into it in static scope too? class x { private: static map var; }; /* The below code will generate compile error. */ /* I wish to have elements to be inserted into map before x is instanced. How I can do so? */ map x::map = { {1, 2}, {3, 4} }; Thanks! cheok -- modified at 5:11 Friday 23rd September, 2005
-
for primitive data type, i am able to initialize them in static scope: class x { public: static int var[]; }; /* We are able to have var initialize before x object being instance. */ int x::var[] = {1, 2, 3}; However, for a stl map, how i can have elements inserted into it in static scope too? class x { private: static map var; }; /* The below code will generate compile error. */ /* I wish to have elements to be inserted into map before x is instanced. How I can do so? */ map x::map = { {1, 2}, {3, 4} }; Thanks! cheok -- modified at 5:11 Friday 23rd September, 2005
where did you see you could access a private member from outside the class like that ? second point : did you have a look at how a map was working ?
class x {
private:
static std::map<your_key_type, your_value_type> var;public:
x();
void InsertInMap(your_key_type k, your_value_type v);
};x::x() {
//Class Initialisations here...
}void x::InsertInMap(your_key_type k, your_value_type v) {
x::var[k] = v;
}void main(void) {
x myX;
x.InsertInMap(1, 2);
x.InsertInMap(3, 4);
}next time, read the docs...
TOXCCT >>> GEII power
[toxcct][VisualCalc] -- modified at 5:26 Friday 23rd September, 2005 -
where did you see you could access a private member from outside the class like that ? second point : did you have a look at how a map was working ?
class x {
private:
static std::map<your_key_type, your_value_type> var;public:
x();
void InsertInMap(your_key_type k, your_value_type v);
};x::x() {
//Class Initialisations here...
}void x::InsertInMap(your_key_type k, your_value_type v) {
x::var[k] = v;
}void main(void) {
x myX;
x.InsertInMap(1, 2);
x.InsertInMap(3, 4);
}next time, read the docs...
TOXCCT >>> GEII power
[toxcct][VisualCalc] -- modified at 5:26 Friday 23rd September, 2005Mmmh, I think you read his post a little bit too fast ;) toxcct wrote: where did you see you could access a private member from outside the class like that That's the way of initializing static member variables. This is done in the cpp file in that way:
int X::MyVar = 0;
Otherwise you will have a linkking error if you don't do it. toxcct wrote: second point : did you have a look at how a map was working ? He was asking how to fill the static map with data the same way you initiliaze the static variable. But unfortunately, I don't know if it's possible. toxcct wrote: next time, read the docs... next time, read the post... ;P;P (just for kidding of course ;)) -
Mmmh, I think you read his post a little bit too fast ;) toxcct wrote: where did you see you could access a private member from outside the class like that That's the way of initializing static member variables. This is done in the cpp file in that way:
int X::MyVar = 0;
Otherwise you will have a linkking error if you don't do it. toxcct wrote: second point : did you have a look at how a map was working ? He was asking how to fill the static map with data the same way you initiliaze the static variable. But unfortunately, I don't know if it's possible. toxcct wrote: next time, read the docs... next time, read the post... ;P;P (just for kidding of course ;))hum, yes, you may be right, but his post was not that clear... cedric moonen wrote: He was asking how to fill the static map with data the same way you initiliaze the static variable. But unfortunately, I don't know if it's possible. yes, that's ot possible. but i still don't understand why he want to initialize like this "before making the instance"...
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
Mmmh, I think you read his post a little bit too fast ;) toxcct wrote: where did you see you could access a private member from outside the class like that That's the way of initializing static member variables. This is done in the cpp file in that way:
int X::MyVar = 0;
Otherwise you will have a linkking error if you don't do it. toxcct wrote: second point : did you have a look at how a map was working ? He was asking how to fill the static map with data the same way you initiliaze the static variable. But unfortunately, I don't know if it's possible. toxcct wrote: next time, read the docs... next time, read the post... ;P;P (just for kidding of course ;))I agree with moonen. Everyone looks daft in a new area in experts eyes. The solution to the problem is to init the map in a constructor:
class CMy { public: CMy(); private: static std::map mymap; static CMy *p; }; CMy* CMy::p = 0; CMy::CMy() { mymap[1] = 1; }
-
I agree with moonen. Everyone looks daft in a new area in experts eyes. The solution to the problem is to init the map in a constructor:
class CMy { public: CMy(); private: static std::map mymap; static CMy *p; }; CMy* CMy::p = 0; CMy::CMy() { mymap[1] = 1; }
However, that need to instance the object first before the static variable can get initialized. I have a suggestion of doing so by using lazy initialization. not so good since it is not really insert the element in static scope. however, this at least still meet my initial need: get element inserted into stl map before the class object instance created. class x { public: static const map& GetMap { static const map m; if(m.size() == 0) { // Perform element insertion here. // } return m; } };:)
-
hum, yes, you may be right, but his post was not that clear... cedric moonen wrote: He was asking how to fill the static map with data the same way you initiliaze the static variable. But unfortunately, I don't know if it's possible. yes, that's ot possible. but i still don't understand why he want to initialize like this "before making the instance"...
TOXCCT >>> GEII power
[toxcct][VisualCalc]That's is a matter of design. Says you have a MathTeacher class, each math teacher instance is suppose to hold a "multiplication table" member variable. since you are quite sure the "multiplication table" will be same everywhere (you always get 4 when you perform 2x2), it make sense to make the multiplication table as static const scope. so, instead of having 10000 copies of MathTeacher objects (hence, 10000 copies of "multiplication table" since each teacher is having one copy of table). by having static scope member variable, we can have 10000 copies of teachers, share one copy of math table, which we are sure it will be always constant and never changed. hope this help.
-
for primitive data type, i am able to initialize them in static scope: class x { public: static int var[]; }; /* We are able to have var initialize before x object being instance. */ int x::var[] = {1, 2, 3}; However, for a stl map, how i can have elements inserted into it in static scope too? class x { private: static map var; }; /* The below code will generate compile error. */ /* I wish to have elements to be inserted into map before x is instanced. How I can do so? */ map x::map = { {1, 2}, {3, 4} }; Thanks! cheok -- modified at 5:11 Friday 23rd September, 2005