Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. initialize stl map in static scope

initialize stl map in static scope

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yccheok
    wrote on last edited by
    #1

    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

    T T 2 Replies Last reply
    0
    • Y yccheok

      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

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      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

      C 1 Reply Last reply
      0
      • T toxcct

        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

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        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 ;))

        T L 2 Replies Last reply
        0
        • C Cedric Moonen

          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 ;))

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          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]

          Y 1 Reply Last reply
          0
          • C Cedric Moonen

            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 ;))

            L Offline
            L Offline
            Laffis
            wrote on last edited by
            #5

            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; }

            Y 1 Reply Last reply
            0
            • L Laffis

              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; }

              Y Offline
              Y Offline
              yccheok
              wrote on last edited by
              #6

              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; } };:)

              1 Reply Last reply
              0
              • T toxcct

                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]

                Y Offline
                Y Offline
                yccheok
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • Y yccheok

                  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

                  T Offline
                  T Offline
                  Tim Smith
                  wrote on last edited by
                  #8

                  never mind, I got stupid. I shouldn't post in the morning. :) Tim Smith I'm going to patent thought. I have yet to see any prior art. -- modified at 9:59 Friday 23rd September, 2005

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups