 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
prem Guest
|
Posted: Fri Apr 29, 2005 8:41 am Post subject: stl map subclassing |
|
|
Hi,
I'm subclassing the map class as follows ..
----------------
template <typename T1,typename T2>
struct ExtendedMap:public map<T1,T2>
{
~ExtendedMap()
{
map<T1,T2>::iterator iter;
for(iter = begin() ; iter!=end() ; iter++)
{
//iter->second.destroy();
}
}
};
typedef ExtendedMap<uint32_t,uint32_t> testmap;
----------------
at the declaration of map<T1,T2>::iterator iter; the g++ compiler is
throwing me this warning.
std::map<T1, T2, std::less<_Key>, std::allocator<std::pair
_Tp> > >::iterator ' is implicitly a typename
But I tried this declaration : iterator iter; as iterator is a public
typedef in map . But it did not even compile saying that iterator is
not recognized.
How do I get around this ???
Thanks.
prem
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sumit Rajan Guest
|
Posted: Fri Apr 29, 2005 4:38 pm Post subject: Re: stl map subclassing |
|
|
"prem" <contactprem (AT) gmail (DOT) com> wrote
| Quote: | Hi,
I'm subclassing the map class as follows ..
----------------
template <typename T1,typename T2
struct ExtendedMap:public map
{
~ExtendedMap()
{
map
for(iter = begin() ; iter!=end() ; iter++)
|
Apart from the correction I suggested in my earlier post, you may also like
to change the above line to:
for(iter = this->begin() ; iter!=this->end() ; iter++)
See:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12
Regards,
Sumit.
--
Sumit Rajan <sumit.rajan (AT) gmail (DOT) com>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sumit Rajan Guest
|
Posted: Fri Apr 29, 2005 4:40 pm Post subject: Re: stl map subclassing |
|
|
"prem" <contactprem (AT) gmail (DOT) com> wrote
| Quote: | Hi,
I'm subclassing the map class as follows ..
----------------
template <typename T1,typename T2
struct ExtendedMap:public map
{
~ExtendedMap()
{
map
|
typename map<T1,T2>::iterator iter;
should take care of your immediate problem.
| Quote: | for(iter = begin() ; iter!=end() ; iter++)
{
//iter->second.destroy();
}
}
};
typedef ExtendedMap<uint32_t,uint32_t> testmap;
----------------
at the declaration of map<T1,T2>::iterator iter; the g++ compiler is
throwing me this warning.
std::map<T1, T2, std::less<_Key>, std::allocator<std::pair
_Tp> > >::iterator ' is implicitly a typename
But I tried this declaration : iterator iter; as iterator is a public
typedef in map . But it did not even compile saying that iterator is
not recognized.
|
Regards,
Sumit.
--
Sumit Rajan <sumit.rajan (AT) gmail (DOT) com>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Sat Apr 30, 2005 11:20 am Post subject: Re: stl map subclassing |
|
|
prem wrote:
| Quote: | Hi,
I'm subclassing the map class as follows ..
----------------
template <typename T1,typename T2
struct ExtendedMap:public map
{
~ExtendedMap()
{
map
for(iter = begin() ; iter!=end() ; iter++)
{
//iter->second.destroy();
}
}
};
typedef ExtendedMap<uint32_t,uint32_t> testmap;
----------------
at the declaration of map<T1,T2>::iterator iter; the g++ compiler is
throwing me this warning.
std::map<T1, T2, std::less<_Key>, std::allocator<std::pair
_Tp> > >::iterator ' is implicitly a typename
|
It's asking you to explicitly use the keyword typename in that
declaration:
typename map<T1,T2>::iterator iter;
BTW, what you're doing is wrong... Plain wrong. One should *never*
derive from standard library container classes. They're not designed
as base classes (e.g., they don't have a virtual destructor), and so
deriving from them is asking for trouble.
You could simply create your class and put an std::map as one of its
data members, and create your member functions that map to std::map's
functions as needed.
HTH,
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nickolay Merkin Guest
|
Posted: Sat Apr 30, 2005 11:21 am Post subject: Re: stl map subclassing |
|
|
| Quote: | How do I get around this ???
|
just add 'typename' :)
typename map<T1,T2>::iterator it;
or reintroduce this typedef
typedef typename map<T1,T2>::iterator iterator;
The reason why the compiler notifies you is
that it cannot be sure that exact instantiation of map<T1,T2>
(it may be specialized!)
has 'iterator' identifier at all, or that it's a typedef.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nickolay Merkin Guest
|
Posted: Sat Apr 30, 2005 11:22 am Post subject: Re: stl map subclassing |
|
|
By the way, if you need to clean data of the map entries,
maybe it is better to store some smart classes instead of original
data?
Something like shared_ptr...
Because otherwise you should take into account all possible ways to
delete entries.
- in the map's destructor
- in erase() and clear()
- probably, in assignment (rewriting old value)
The last case is worst, because map::operator[] returns a reference to
a value,
and it doesn't know if you just read or rewrite it.
To eliminate this problem, you could replace it, and return a proxy
object
with overloaded operator=()... but it is not enough:
one can retrieve a T2& and modify it somewhere else.
Thus, the best solution is using smart objects such as shared_ptr.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Sat Apr 30, 2005 11:31 am Post subject: Re: stl map subclassing |
|
|
prem wrote:
| Quote: | Hi,
I'm subclassing the map class as follows ..
----------------
template <typename T1,typename T2
struct ExtendedMap:public map
{
~ExtendedMap()
{
map
for(iter = begin() ; iter!=end() ; iter++)
{
//iter->second.destroy();
}
}
};
typedef ExtendedMap<uint32_t,uint32_t> testmap;
----------------
at the declaration of map<T1,T2>::iterator iter; the g++ compiler is
throwing me this warning.
std::map<T1, T2, std::less<_Key>, std::allocator<std::pair
_Tp> > >::iterator ' is implicitly a typename
|
I wrote an explanation of this at
<http://womble.decadentplace.org.uk/c++/template-faq.html#disambiguation>.
| Quote: | But I tried this declaration : iterator iter; as iterator is a public
typedef in map . But it did not even compile saying that iterator is
not recognized.
|
This behaviour is correct; see
<http://womble.decadentplace.org.uk/c++/template-faq.html#base-lookup>.
| Quote: | How do I get around this ???
|
Add "typename" before the declaration of iter.
--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by
<http://womble.decadentplace.org.uk/c++/template-faq.html>.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stephen Howe Guest
|
Posted: Wed May 04, 2005 8:12 am Post subject: Re: stl map subclassing |
|
|
| Quote: | BTW, what you're doing is wrong... Plain wrong. One should *never*
derive from standard library container classes.
|
You mean, one should never publically derive from standard library container
classes (because of the point you mentioned). Nothing wrong with protected
or private derivation.
Stephen Howe
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|