class with a map

M

mosfet

Hi,

I would like to write a class storing http headers so Istarted with this :

class WebHeaderCollection
{
private:
std::map<tstring, tstring> m_KeyValueMap;

public:

tstring& operator[](const tstring& Keyval)
{
tstring sTmp = Keyval + _T("\r\n");
return m_KeyValueMap[ Keyval ];
}
};


This class appends \r\n to each key entry.
My question is how can I give access to all the standard operations of a
map.
The simple solution would have been to derive from std::map but it seems
to be a bad idea to derive from STL containers.

So does it mean I need to provide an iterator, an overload operator like
I did, ...

Do you have some example?
 
D

DJ

mosfet napisał(a):
Hi,

I would like to write a class storing http headers so Istarted with this :

class WebHeaderCollection
{
private:
std::map<tstring, tstring> m_KeyValueMap;

public:

tstring& operator[](const tstring& Keyval)
{
tstring sTmp = Keyval + _T("\r\n");
return m_KeyValueMap[ Keyval ];
}
};


This class appends \r\n to each key entry.
My question is how can I give access to all the standard operations of a
map.
The simple solution would have been to derive from std::map but it seems
to be a bad idea to derive from STL containers.

So does it mean I need to provide an iterator, an overload operator like
I did, ...

1. std::map<tstring, tstring> get_http_headers() member to access
private map member

2. Class WebHeaderCollection : public std::map<tstring, tstring>
{ ... and you have iterators there
 
G

Guest

Hi,

I would like to write a class storing http headers so Istarted with this :

class WebHeaderCollection
{
private:
std::map<tstring, tstring> m_KeyValueMap;

public:

tstring& operator[](const tstring& Keyval)
{
tstring sTmp = Keyval + _T("\r\n");
return m_KeyValueMap[ Keyval ];
}
};


This class appends \r\n to each key entry.
My question is how can I give access to all the standard operations of a
map.
The simple solution would have been to derive from std::map but it seems
to be a bad idea to derive from STL containers.

So does it mean I need to provide an iterator, an overload operator like
I did, ...

If all your class does that std::map does not is to add \r\n to the key
before lookup then perhaps you should consider storing the key without
\r\n instead.
 
M

mosfet

DJ a écrit :
mosfet napisał(a):
Hi,

I would like to write a class storing http headers so Istarted with
this :

class WebHeaderCollection
{
private:
std::map<tstring, tstring> m_KeyValueMap;

public:

tstring& operator[](const tstring& Keyval)
{
tstring sTmp = Keyval + _T("\r\n");
return m_KeyValueMap[ Keyval ];
}
};


This class appends \r\n to each key entry.
My question is how can I give access to all the standard operations of
a map.
The simple solution would have been to derive from std::map but it
seems to be a bad idea to derive from STL containers.

So does it mean I need to provide an iterator, an overload operator
like I did, ...

1. std::map<tstring, tstring> get_http_headers() member to access
private map member

2. Class WebHeaderCollection : public std::map<tstring, tstring>
{ ... and you have iterators there
Thanks but solution 1 is not suitable in my case because I also need to
prevent user to add specific keys (accept, content-type, ...)


And in solution 2, I was told it's bad to derive from container...


That's why I was thinking of writing a class with a map inside.
 
D

Daniel Kay

mosfet said:
DJ a écrit :
mosfet napisał(a):
Hi,

I would like to write a class storing http headers so Istarted with
this :

class WebHeaderCollection
{
private:
std::map<tstring, tstring> m_KeyValueMap;

public:

tstring& operator[](const tstring& Keyval)
{
tstring sTmp = Keyval + _T("\r\n");
return m_KeyValueMap[ Keyval ];
}
};


This class appends \r\n to each key entry.
My question is how can I give access to all the standard operations
of a map.
The simple solution would have been to derive from std::map but it
seems to be a bad idea to derive from STL containers.

So does it mean I need to provide an iterator, an overload operator
like I did, ...

1. std::map<tstring, tstring> get_http_headers() member to access
private map member

2. Class WebHeaderCollection : public std::map<tstring, tstring>
{ ... and you have iterators there
Thanks but solution 1 is not suitable in my case because I also need to
prevent user to add specific keys (accept, content-type, ...)


And in solution 2, I was told it's bad to derive from container...

That's why I was thinking of writing a class with a map inside.

You can't change any values when you use the first solution. It returns
a copied version of the map. If this is a performance issue, you could
return a const reference to the internal map. The user can read values
at will.

Why is it a bad idea to derive from map. And why is it a bad idea if you
want to have access to all std::map methods? You could write wrapper
methods, which call the std::map methods. But why should you do that?

If you only want to export a few methods, then try the following
attempt. I am quite sure it won't compile (not tested), but it's just
that you get the point:

template<K,V> class WebHeaderCollection {
private:
std::map<K,V> m_values;

public:
typename std::map<K,V>::iterator iterator;
iterator begin() { return m_values.begin(); }
iterator end() { return m_values.end(); }
};
 
T

tragomaskhalos

Hi, snip
This class appends \r\n to each key entry.
My question is how can I give access to all the standard operations of a
map.
snip

How about something like this:

class map {
public:
void map_func_a();
void map_func_b();
void map_func_c();
};

class more_than_map : private map {
public:
using map::map_func_a;
using map::map_func_b;
void other_func();
};

int main()
{
more_than_map mm;
mm.map_func_a();
mm.map_func_b();
// mm.map_func_c(); can't call this one
}
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,266
Latest member
DavidaAlla

Latest Threads

Top