reference to const static object variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Doe

    reference to const static object variable

    I have a const static object. What is the right syntax to get a
    reference to the std::vector within the std::map variable?:

    class MyObj
    {
    public:
    ...
    std::map<std::s tring,
    std::vector<std ::string l_;
    };

    static MyObj obj;

    int main()
    {
    ...
    const std::vector<std ::string&regexL ist = obj.l_["key"]; //
    compiler error
    ...
    };



  • Obnoxious User

    #2
    Re: reference to const static object variable

    On Fri, 14 Nov 2008 11:23:28 -0800, Bob Doe wrote:
    I have a const static object. What is the right syntax to get a
    reference to the std::vector within the std::map variable?:
    >
    class MyObj
    {
    public:
    ...
    std::map<std::s tring,
    std::vector<std ::string l_;
    };
    >
    static MyObj obj;
    >
    int main()
    {
    ...
    const std::vector<std ::string&regexL ist = obj.l_["key"]; //
    compiler error
    ...
    };
    Did you care to read the error message? What did it say?

    --
    OU
    Remember 18th of June 2008, Democracy died that afternoon.

    Comment

    • Bob Doe

      #3
      Re: reference to const static object variable

      sorry,

      The example should have been:

      class MyObj
      {
      public:
      MyObj()
      {
      std::vector<std ::stringt;
      t.push_back("zz z");
      l_["a"] = t;
      }

      std::map<std::s tring,
      std::vector<std ::string l_;
      };

      const static MyObj obj;

      int main()
      {
      const std::vector<std ::string&regexL ist = obj.l_["key"];
      }


      -----
      I want to maintain the const-ness of MyObj, and have read access to
      the std::vector within std::map...

      and the error message says:

      passing `const
      map<basic_strin g<char,string_c har_traits<char >,__default_all oc_template<fal se,
      0>
      >,vector<basic_ string<char,str ing_char_traits <char>,__defaul t_alloc_templat e<false,
      0>
      >,allocator<bas ic_string<char, string_char_tra its<char>,__def ault_alloc_temp late<false,
      0 >
      >,less<basic_st ring<char,strin g_char_traits<c har>,__default_ alloc_template< false,
      0
      >,allocator<vec tor<basic_strin g<char,string_c har_traits<char >,__default_all oc_template<fal se,
      0>
      >,allocator<bas ic_string<char, string_char_tra its<char>,__def ault_alloc_temp late<false,
      0 ' as `this' argument of `class
      vector<basic_st ring<char,strin g_char_traits<c har>,__default_ alloc_template< false,
      0>
      >,allocator<bas ic_string<char, string_char_tra its<char>,__def ault_alloc_temp late<false,
      0 &
      map<basic_strin g<char,string_c har_traits<char >,__default_all oc_template<fal se,
      0>
      >,vector<basic_ string<char,str ing_char_traits <char>,__defaul t_alloc_templat e<false,
      0>
      >,allocator<bas ic_string<char, string_char_tra its<char>,__def ault_alloc_temp late<false,
      0 >
      >,less<basic_st ring<char,strin g_char_traits<c har>,__default_ alloc_template< false,
      0
      >,allocator<vec tor<basic_strin g<char,string_c har_traits<char >,__default_all oc_template<fal se,
      0>
      >,allocator<bas ic_string<char, string_char_tra its<char>,__def ault_alloc_temp late<false,
      0 ::operator [](const string &)' discards qualifiers


      Which I don't understand.

      const static MyObj obj;

      On Nov 14, 12:30 pm, Obnoxious User <O...@127.0.0.1 wrote:
      On Fri, 14 Nov 2008 11:23:28 -0800, Bob Doe wrote:
      I have a const static object.  What is the right syntax to get a
      reference to the std::vector within the std::map variable?:
      >
      class MyObj
      {
         public:
           ...
           std::map<std::s tring,
                        std::vector<std ::string l_;
      };
      >
       static MyObj obj;
      >
      int main()
      {
        ...
        const std::vector<std ::string&regexL ist = obj.l_["key"];  //
      compiler error
        ...
      };
      >
      Did you care to read the error message? What did it say?
      >
      --
      OU
      Remember 18th of June 2008, Democracy died that afternoon.http://frapedia..se/wiki/Information_in_English

      Comment

      • James Kanze

        #4
        Re: reference to const static object variable

        On Nov 14, 10:24 pm, Bob Doe <DumpForJ...@gm ail.comwrote:
        sorry,
        The example should have been:
        class MyObj
        {
           public:
              MyObj()
              {
                 std::vector<std ::stringt;
                 t.push_back("zz z");
                 l_["a"] = t;
              }
              std::map<std::s tring,
                       std::vector<std ::string l_;
        };
        const static MyObj obj;
        int main()
        {
           const std::vector<std ::string&regexL ist = obj.l_["key"];
        Have you looked at the documentation of std::map<>:oper ator[]?
        What does it do?

        Since it modifies the map, you can't use it on a const object.
        }
        I want to maintain the const-ness of MyObj, and have read
        access to the std::vector within std::map...
        Then operator[] isn't the function you want. Something like:


        std::map< std::string, std::vector< std::string >
        >::const_iterat or
        entry = obj.l_[ "key" ] ;
        if ( entry != obj.l_.end() ) {
        std::vector< std::string const&
        regexList = entry->second ;
        // ...
        }

        maybe. (Or maybe std::map isn't what you really want, except
        buried deep in the implementation of something with a more
        congenial interface.)

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • Obnoxious User

          #5
          Re: reference to const static object variable

          On Fri, 14 Nov 2008 14:59:46 -0800, James Kanze wrote:
          On Nov 14, 10:24 pm, Bob Doe <DumpForJ...@gm ail.comwrote:
          >sorry,
          >
          [snip]
          >
          >I want to maintain the const-ness of MyObj, and have read access to the
          >std::vector within std::map...
          >
          Then operator[] isn't the function you want. Something like:
          >
          >
          std::map< std::string, std::vector< std::string >
          >>::const_itera tor
          entry = obj.l_[ "key" ] ;
          const_iterator entry = obj.find("key") ;
          if ( entry != obj.l_.end() ) {
          std::vector< std::string const&
          regexList = entry->second ;
          // ...
          }
          >
          --
          OU
          Remember 18th of June 2008, Democracy died that afternoon.

          Comment

          • James Kanze

            #6
            Re: reference to const static object variable

            On Nov 15, 9:11 am, Obnoxious User <O...@127.0.0.1 wrote:
            On Fri, 14 Nov 2008 14:59:46 -0800, James Kanze wrote:
            On Nov 14, 10:24 pm, Bob Doe <DumpForJ...@gm ail.comwrote:
            sorry,
            [snip]
            I want to maintain the const-ness of MyObj, and have read
            access to the std::vector within std::map...
            Then operator[] isn't the function you want.  Something like:
                std::map< std::string, std::vector< std::string >
            >::const_iterat or
                                    entry = obj.l_[ "key"] ;
            const_iterator entry = obj.find("key") ;
            Yes, obviously.
                if ( entry != obj.l_.end() ) {
                    std::vector< std::string const&
                                        regexList = entry->second ;
                    // ...
                }
            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            Working...