473,324 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

static MyObj obj;

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

Nov 14 '08 #1
5 3041
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
Nov 14 '08 #2
sorry,

The example should have been:

class MyObj
{
public:
MyObj()
{
std::vector<std::stringt;
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 ::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.1wrote:
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
Nov 14 '08 #3
On Nov 14, 10:24*pm, Bob Doe <DumpForJ...@gmail.comwrote:
sorry,
The example should have been:
class MyObj
{
* *public:
* * * MyObj()
* * * {
* * * * *std::vector<std::stringt;
* * * * *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<>: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:ja*********@gmail.com
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
Nov 14 '08 #4
On Fri, 14 Nov 2008 14:59:46 -0800, James Kanze wrote:
On Nov 14, 10:24Â*pm, Bob Doe <DumpForJ...@gmail.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_iterator
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.
http://frapedia.se/wiki/Information_in_English
Nov 15 '08 #5
On Nov 15, 9:11*am, Obnoxious User <O...@127.0.0.1wrote:
On Fri, 14 Nov 2008 14:59:46 -0800, James Kanze wrote:
On Nov 14, 10:24*pm, Bob Doe <DumpForJ...@gmail.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_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:ja*********@gmail.com
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
Nov 15 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Asfand Yar Qazi | last post by:
Hi, Could anyone explain to me why this works? I can't figure out the wonky const syntax! class Surface { ... protected:
10
by: sam | last post by:
Hi, I m wondering why I can't declare reference variable for default value in a function argument variable? eg. class A { void f(string &str="");
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
10
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in...
13
by: Amadeus W. M. | last post by:
I have a member static const int x defined in class Foo, and I'm passing it by reference, and by value elsewhere (see the code below). Passing it by value works, but by reference it doesn't: it...
8
by: bipod.rafique | last post by:
Hello All, I need your help in understanding something. I have a simple class class test{ };
5
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can...
18
by: Dejfson | last post by:
Dear All, can someone clarify me how to return the reference to the empty object in case of error? _not working_ Example of what i'd like to do: const MyClassData& MyClass () {...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.