473,410 Members | 1,946 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,410 software developers and data experts.

getting values in a map - map wrapper class

I am writing a class as a wrapper around a std::map.

The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>

At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]

Eg if I create a:
CGeneralMap<int, std::stringTestMap;

and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:

value = [3];

but that doesn't work.

Oct 1 '07 #1
12 3546
On Oct 2, 12:28 am, Angus <anguscom...@gmail.comwrote:
I am writing a class as a wrapper around a std::map.

The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>

At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]

Eg if I create a:
CGeneralMap<int, std::stringTestMap;

and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?

In my GetValue function I tried:

value = [3];

but that doesn't work.

std::string getKeyValue( int index ) const
{
// Code...

return TestMap[ index ];
}

--
Chris Val

Oct 1 '07 #2
On 1 Oct, 15:38, "Chris ( Val )" <chris...@gmail.comwrote:
On Oct 2, 12:28 am, Angus <anguscom...@gmail.comwrote:


I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int, std::stringTestMap;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
value = [3];
but that doesn't work.

std::string getKeyValue( int index ) const
{
// Code...

return TestMap[ index ];
}

--
Chris Val- Hide quoted text -

- Show quoted text -
Yes of course I understand that. But I am talking about from within
my class. From within my GetValue function.
Oct 1 '07 #3
On Oct 2, 1:04 am, Angus <anguscom...@gmail.comwrote:
On 1 Oct, 15:38, "Chris ( Val )" <chris...@gmail.comwrote:


On Oct 2, 12:28 am, Angus <anguscom...@gmail.comwrote:
I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int, std::stringTestMap;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
value = [3];
but that doesn't work.
std::string getKeyValue( int index ) const
{
// Code...
return TestMap[ index ];
}
--
Chris Val- Hide quoted text -
- Show quoted text -

Yes of course I understand that. But I am talking about from within
my class. From within my GetValue function.- Hide quoted text -
What am I missing?
What I showed you can be a member function if you put it in a class.

--
Chris Val

Oct 1 '07 #4
Angus wrote:
I am writing a class as a wrapper around a std::map.

The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>

At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]

Eg if I create a:
CGeneralMap<int, std::stringTestMap;

and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:

value = [3];

but that doesn't work.
You could of course use
value = (*this)[3];

But I think the "optimal" solution will be
value = operator[](3);

Both of those call the "operator[]" of the std::map, which is what you want.

Cheers,
Daniel
--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!
Oct 1 '07 #5
"Chris ( Val )" <chris...@gmail.comwrote:
std::string getKeyValue( int index ) const
{
// Code...

return TestMap[ index ];
Can std::map<operator [](size_t) be used in a const member function?
}
Oct 1 '07 #6
On 1 Oct, 18:32, Daniel Kraft <d...@domob.euwrote:
Angus wrote:
I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int, std::stringTestMap;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
value = [3];
but that doesn't work.

You could of course use
value = (*this)[3];

But I think the "optimal" solution will be
value = operator[](3);

Both of those call the "operator[]" of the std::map, which is what you want.

Cheers,
Daniel

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!- Hide quoted text -

- Show quoted text -
Ah brilliant, that is what I needed. But now I have a conversion
problem.

If on line 366 I have:
Value = operator[](Key);

and on line 367 I have:
Value = (*this)[Key];

Then I get these compilation errors in Microsoft Visual C++ version 6:
TestBed.cpp
generalmap.h(366) : error C2662: '[]' : cannot convert 'this' pointer
from
'const class CGeneralMap<int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char >'

to

'class std::map<int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char,struct
std::less<int>,class std::allocator<class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char &'

Conversion loses qualifiers
c:\program files\microsoft visual studio\vc98\include
\xmemory(59) : while compiling class-template member function 'void
__thiscall CGeneralMap<int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char >::G
etValue(const int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char &) const'

generalmap.h(367) : error C2678: binary '[' : no operator defined
which takes a left-hand operand of type 'const class
CGeneralMap<int,class std::basic_string<char,struct std::char_traits<
char>,class std::allocator<char >' (or there is no acceptable
conversion)
c:\program files\microsoft visual studio\vc98\include
\xmemory(59) : while compiling class-template member function 'void
__thiscall CGeneralMap<int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char >::G
etValue(const int,class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char &) const'

ie I am trying both options.

Oct 1 '07 #7
On 1 Oct, 18:32, Daniel Kraft <d...@domob.euwrote:
Angus wrote:
I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int, std::stringTestMap;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
value = [3];
but that doesn't work.

You could of course use
value = (*this)[3];

But I think the "optimal" solution will be
value = operator[](3);

Both of those call the "operator[]" of the std::map, which is what you want.

Cheers,
Daniel

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!- Hide quoted text -

- Show quoted text -
Its ok I had to fiddle with const qualifiers. It now works fine.

Oct 1 '07 #8
On Oct 2, 1:04 am, Angus <anguscom...@gmail.comwrote:
On 1 Oct, 15:38, "Chris ( Val )" <chris...@gmail.comwrote:


On Oct 2, 12:28 am, Angus <anguscom...@gmail.comwrote:
I am writing a class as a wrapper around a std::map.
The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>
At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]
Eg if I create a:
CGeneralMap<int, std::stringTestMap;
and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:
value = [3];
but that doesn't work.
std::string getKeyValue( int index ) const
{
// Code...
return TestMap[ index ];
}

Yes of course I understand that. But I am talking about from within
my class. From within my GetValue function.
In future, please try to describe what you're having difficulty
with a bit more clearly. You stated that you were having difficulty
with your member function "GetValue", not operator[] which is quite
different.

While you're at it, ensure that you check for the key's existence.

--
Chris Val



Oct 1 '07 #9
On Oct 2, 1:35 am, yanlinli...@gmail.com wrote:
"Chris ( Val )" <chris...@gmail.comwrote:
std::string getKeyValue( int index ) const
{
// Code...
return TestMap[ index ];

Can std::map<operator [](size_t) be used in a const member function?
I don't think so.

--
Chris Val

Oct 1 '07 #10
On 2007-10-01 16:28, Angus wrote:
I am writing a class as a wrapper around a std::map.

The class is:
template<class TKey, class TValue>
class CGeneralMap : protected map<TKey, TValue>

At the moment I have basic functions like Add, Remove etc but I am
really struggling with GetValue, which will be like the value obtained
from [key]

Eg if I create a:
CGeneralMap<int, std::stringTestMap;

and I have a key 3 with std::string value of John. then how in my
GetValue function do I extract the "John"?
In my GetValue function I tried:

value = [3];

but that doesn't work.

You should use the find() method for a GetValue method since there is no
guarantee that the key specified exists. Using the [] operator will
create an entry if it does not exist which is not what you want from a
GetValue method.

--
Erik Wikström
Oct 1 '07 #11
On Oct 2, 2:42 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-10-01 16:28, Angus wrote:
[snip]
In my GetValue function I tried:
value = [3];
but that doesn't work.

You should use the find() method for a GetValue method since there is no
guarantee that the key specified exists. Using the [] operator will
create an entry if it does not exist which is not what you want from a
GetValue method.
That was exactly the thought I had, which led me to believe
the OP didn't want operator[].

--
Chris Val

Oct 1 '07 #12
On Oct 1, 5:35 pm, yanlinli...@gmail.com wrote:
"Chris ( Val )" <chris...@gmail.comwrote:
std::string getKeyValue( int index ) const
{
// Code...
return TestMap[ index ];
Can std::map<operator [](size_t) be used in a const member function?
}
Of course not.

The first problem is that he is deriving from std::map, when he
probably should be using containment; derivation is a very poor
way to "wrap" something. But having an std::map as a member
doesn't change the problem: you still can't use operator[] in a
const function.

The real question, of course, is what should happen if the value
isn't present in the map. Depending on what the map is used
for, several possibilities can be considered:

-- It's an error. The user should check beforehand (e.g. with
a contains() function which you provide). If the value
isn't present, it's an assertion failure, or possibly only
an exception.

-- It's an expected condition. The simplest solution here is
to return a pointer, returning NULL if the element isn't
present.

-- The element is by definition present, with a default value.
Basically, you implement something like the precedent, but
return "ptr == NULL ? defaultValue : *ptr".

-- The element is automatically inserted, with a default value.
This is the behavior of std::map, but it means that you
cannot read a const map.

The function behind whatever you do (except for the last) is
std::map<>::find. For the second solution (which is the most
general, and easiest to map into the other solutions), you do
something like:

ValueType const*
Map::get( KeyType const& key ) const
{
std::map<...>::const_iterator
elem = myMap.find( key ) ;
return elem == myMap.end()
? NULL
: &elem->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

Oct 2 '07 #13

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

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
12
by: Egil M?ller | last post by:
Is there any way to create transparent wrapper objects in Python? I thought implementing __getattribute__ on either the wrapper class or its metaclass would do the trick, but it does not work for...
8
by: Philip Lawatsch | last post by:
Hi, I'd like to get rid of all the cout stuff in my code. Following situation: Way over 2000 files, search and replace in all of them is not an option. All of the files include a basic...
1
by: tg.foobar | last post by:
my setup: visual studio 2005 sql server 2000 i'm using a dataset (used to be called typed dataset in 2003), where i use the MSDataSetGenerator to create a class for me based on the scheme of...
16
by: utab | last post by:
Dear all, In programming terminology, what is a wrapper and where is it used? Regards
3
by: winkatl1213 | last post by:
Hello, I am working with comtypes to interface Microsoft's DirectShow library. First, I found a Type Library on the internet that was created for accessing DirectShow from .NET. It seems that...
7
by: pooba53 | last post by:
I am working with VB .NET 2003. Let's say my main form is called Form1. I have to launch a new form (Form2) that gathers input from the user. How can I pass variable information back to Form1...
5
by: Larry Bud | last post by:
I'm writing a class to create a specifically formatted fixed width file. It's 800 characters wide, consisting of approx 30 fields. So I need to pass 30 variables, maybe 10 are required. Should...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.