reference to const static object variable

B

Bob Doe

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::string,
std::vector<std::string> > l_;
};

static MyObj obj;

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

Bob Doe

sorry,

The example should have been:

class MyObj
{
public:
MyObj()
{
std::vector<std::string> t;
t.push_back("zzz");
l_["a"] = t;
}

std::map<std::string,
std::vector<std::string> > l_;
};

const static MyObj obj;

int main()
{
const std::vector<std::string> &regexList = 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 said:
,vector<basic_string<char,string_char_traits<char>,__default_alloc_template<false, 0>
,allocator<basic_string<char,string_char_traits<char>,__default_alloc_template<false, 0> > >
,less<basic_string<char,string_char_traits<char>,__default_alloc_template<false, 0> >
,allocator<vector<basic_string<char,string_char_traits<char>,__default_alloc_template<false, 0>
,allocator<basic_string<char,string_char_traits<char>,__default_alloc_template<false,
0> > > > > >' as `this' argument of `class
vector said:
,allocator<basic_string<char,string_char_traits<char>,__default_alloc_template<false,
0> > > > &
map said:
,vector<basic_string<char,string_char_traits<char>,__default_alloc_template<false, 0>
,allocator<basic_string<char,string_char_traits<char>,__default_alloc_template<false, 0> > >
,less<basic_string<char,string_char_traits<char>,__default_alloc_template<false, 0> >
,allocator<vector<basic_string<char,string_char_traits<char>,__default_alloc_template<false, 0>
,allocator<basic_string<char,string_char_traits<char>,__default_alloc_template<false,
0> > > > > >::eek:perator [](const string &)' discards qualifiers


Which I don't understand.

const static MyObj obj;

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::string,
                  std::vector<std::string> > l_;
};
 static MyObj obj;
int main()
{
  ...
  const std::vector<std::string> &regexList = obj.l_["key"];  //
compiler error
  ...
};

Did you care to read the error message? What did it say?
 
J

James Kanze

The example should have been:
class MyObj
{
   public:
      MyObj()
      {
         std::vector<std::string> t;
         t.push_back("zzz");
         l_["a"] = t;
      }
      std::map<std::string,
               std::vector<std::string> > l_;
};
const static MyObj obj;
int main()
{
   const std::vector<std::string> &regexList = obj.l_["key"];

Have you looked at the documentation of std::map<>:eek:perator[]?
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 said:
::const_iterator
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top