Re: reference to const static object variable
On Nov 14, 10:24 pm, Bob Doe <DumpForJ...@gmail.com> wrote:
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> ®exList = obj.l_["key"];
Have you looked at the documentation of std::map<>:operator[]?
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_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.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34