Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reference to const static object variable

Reply
Thread Tools

reference to const static object variable

 
 
Bob Doe
Guest
Posts: n/a
 
      11-14-2008
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
...
};



 
Reply With Quote
 
 
 
 
Bob Doe
Guest
Posts: n/a
 
      11-14-2008
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<basic_string<char,string_char_traits<char>,__d efault_alloc_template<false,
0>
>,vector<basic_string<char,string_char_traits<char >,__default_alloc_template<false,

0>
>,allocator<basic_string<char,string_char_traits<c har>,__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_t raits<char>,__default_alloc_template<false,

0>
>,allocator<basic_string<char,string_char_traits<c har>,__default_alloc_template<false,

0> > > > > >' as `this' argument of `class
vector<basic_string<char,string_char_traits<char>, __default_alloc_template<false,
0>
>,allocator<basic_string<char,string_char_traits<c har>,__default_alloc_template<false,

0> > > > &
map<basic_string<char,string_char_traits<char>,__d efault_alloc_template<false,
0>
>,vector<basic_string<char,string_char_traits<char >,__default_alloc_template<false,

0>
>,allocator<basic_string<char,string_char_traits<c har>,__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_t raits<char>,__default_alloc_template<false,

0>
>,allocator<basic_string<char,string_char_traits<c har>,__default_alloc_template<false,

0> > > > > >:perator [](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::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?
>
> --
> OU
> Remember 18th of June 2008, Democracy died that afternoon.http://frapedia..se/wiki/Information_in_English


 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      11-14-2008
On Nov 14, 10:24*pm, Bob Doe <(E-Mail Removed)> 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> &regexList = obj.l_["key"];


Have you looked at the documentation of std::map<>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< 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:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-15-2008
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 <(E-Mail Removed)> wrote:
> >> 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_iterator

> > * * * * * * * * * * * * 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:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static const T vs static T const er C++ 3 04-22-2008 10:22 PM
non-const reference and const reference George2 C++ 10 12-17-2007 02:19 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
const static Vs. static const Dave C++ 10 05-22-2005 10:32 PM
About static const members appearing in another static const definitions Rakesh Sinha C++ 4 01-13-2005 08:11 AM



Advertisments