Re: CMAP under vs2005+
Tommy wrote:
I'm an old school programmer with 20+ years of basic level C/C++
knowledge so I always thought that adding a class "wrapper" to an
existing object which contains virtual methods was the basic act in
polymorphism. I have a lot of polymorphism in my code. I have a lot of
virtual I/O interfaces, for examples. Lots of long stable code long
engineered for reusability.
With the current compilers being more "template driven", I think my lack
of expertise in templates does not help, which to me, I already felt it
was a matter and decision of mixing "more expanded code" thus the basic
idea of a Template versus reusability, virtual tables, etc.
My problem now is all my code engineered over many years is "suddenly"
broken under VS2005. Some are legit strong(er) C/C++ syntax
highlights. Others compiler errors (and warnings) are less obvious.
So I am going to a period of tying to understand the subtle differences
and why I need to make changes. Sometimes you just made the "change"
necessary, and generally, you shoot for transparency but this is now
more than a simple "change" as it now may alter some fundamental
engineering.
For example, I have code which uses CMapEx in a class object,
class TMailHost {
public:
// Now using new mapex.h
//CMapEx<CString, const char *> inTrans;
CMapEx<CString> inTrans;
};
and functions, including multi-threaded code that use it like so:
BOOL FuncXYZ(const TMailHost &mh)
{
...
CString v;
if (mh.inTrans.Lookup("SomeKey",v)) {
....
}
}
BOOL FuncABC(void *p)
{
...
const TMailHost &mh = *(TMailHost *)p;
CString v;
if (mh.inTrans.Lookup("SomeKey",v)) {
....
}
}
well, the compiler (old and new) is now complaining with the new CMapEx:
error C2662: 'Lookup' : cannot convert 'this' pointer from
'const class CMapEx<class CString>' to
'class CMapEx<class CString> &'
Conversion loses qualifiers
I even tried it with your dictionary.h version.
Changing the function prototype to
BOOL wcUser(TMailHost &mh)
resolves this, but I need to know why before changes are made globally.
I know right now that some code that uses the new CMapEx is
intermittently now producing odd results.
So I think that I need to take a step back and further study the usage
of std c/c++ containers and classes, especially the mixed usages with
MFC and also its history in multi-threaded environment, i.e., how
thread-safe are they?
Anyway, thanks. I have to understand the "why's" above and any
repercussions before I can make use of std classes/templates.
Tommy:
Adding a wrapper to an existing class with virtual methods does not exhibit
polymorphism. You get polymorphism by deriving from a class with virtual
methods, and then accessing the methods via a base class pointer.
But CMap does not have virtual methods, so your original CMapEx deriving from
CMap does not exhibit polymorphism. In fact there is no reason why the names of
the methods of your CMapEx should be the same as the ones of CMap; they are not
overrides. In fact it is bad style to replace/hide base class methods in this way.
But your problem does not, I think, have to do with this. It looks to me that
the Lookup() method of CMapEx is not const. I am confused about what version of
CMapEx you are using. For your original one, the Lookup method was const (as it
is in CMap), but I see that in Giovanni's version it is not (though I think it
should be).
--
David Wilkinson
Visual C++ MVP