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

class with a map

Hi,

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

class WebHeaderCollection
{
private:
std::map<tstring, tstringm_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?

Sep 24 '07 #1
5 1724
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, tstringm_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, tstringget_http_headers() member to access
private map member

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

Sep 24 '07 #2
On 2007-09-24 14:21, mosfet wrote:
Hi,

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

class WebHeaderCollection
{
private:
std::map<tstring, tstringm_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.

--
Erik Wikström
Sep 24 '07 #3
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, tstringm_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, tstringget_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.
Sep 24 '07 #4
mosfet schrieb:
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, tstringm_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, tstringget_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,Vclass WebHeaderCollection {
private:
std::map<K,Vm_values;

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

--
CU,
Daniel

--
Regards,
Daniel
Sep 24 '07 #5
On 24 Sep, 13:21, mosfet <john....@anonymous.orgwrote:
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
}
Sep 24 '07 #6

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,...
18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.