Thank you, each of you--- I really appreciate your generous help with
this. I was initially going to post a test example except that it
relies on the MyString definition, etc. and I don't see a manageable
(for you folks) way to attach files here without posting 7K of text
into a message (which is tough for you to read).
So for starters, perhaps I can just use excerpts where the problem
likely lies, namely the copy constructor and overloaded operator<. I'm
happy to email compilable source code to anyone interested or provide
any other information. But hopefully I'm just making some simple
mistake in these specific areas....
- Imagine XString as a class with std::string _container as a private
member.
- The copy constructor, operator<, and operator= are defined as
follows:
XString::XString(XString& str) :
_container( str._container )
{
};
bool XString::operator<(const XString& str)
{
return (_container < str._container);
};
// assignment operator from a char*
XString& XString::operator=(const char* cs)
{
_container = cs;
return *this;
};
// assignment operator from an STL std::string
XString& XString::operator=(const std::string& str)
{
_container = str;
return *this;
};
// assignment operator from another XString
XString& XString::operator=(const XString& str)
{
_container = str._container;
return *this;
};
Then the test code (in a main() block) looks like this:
map< XString, XString > xsmap;
map< XString, XString >::iterator xsmapItr;
XString t1("john");
XString t2("annakin");
XString t3("george");
xsmap["hello"] = "world";
xsmap[t1] = "adams";
xsmap[t2] = "skywalker";
xsmap[t3] = "lucas";
xsmap["john"] = "doe";
xsmap[t1] = "something else";
xsmapItr = xsmap.find("hello");
if ( xsmapItr == xsmap.end() )
cout << "xstring not found" << endl;
== OUTPUTS: xstring not found
for ( xsmapItr = xsmap.begin() ; xsmapItr != xsmap.end();
++xsmapItr )
cout << xsmapItr->first << " : " << xsmapItr->second << endl;
==OUTPUTS:
john : adams
hello : world
annakin : skywalker
george : lucas
john : something else
john : doe
cout << t1 << " < " << t2 << " : " << ( t1 < t2 ? true : false ) <<
endl;
cout << t1 << " < " << t3 << " : " << ( t1 < t3 ? true : false ) <<
endl;
cout << t2 << " < " << t1 << " : " << ( t2 < t1 ? true : false ) <<
endl;
cout << t2 << " < " << t3 << " : " << ( t2 < t3 ? true : false ) <<
endl;
cout << t3 << " < " << t1 << " : " << ( t3 < t1 ? true : false ) <<
endl;
cout << t3 << " < " << t2 << " : " << ( t3 < t2 ? true : false ) <<
endl;
==OUTPUTS:
john < annakin : 0
john < george : 0
annakin < john : 1
annakin < george : 1
george < john : 1
george < annakin : 0
So... note the correct results for the < tests, plus note that
iterating through the map shows three results for "john".... (ie. this
isn't a destruction-on-copy operation as a previous poster suggested,
but all three exist in the map simultaneously). But all assignments
and comparisons seem to work correctly, suggesting the copy constructor
and operator< are working as expected. Also, I have operators and
methods all provide for const char* cs, std::string& str, and XString&
str for all possible operations.
Thanks so much for your help with this. Hopefully I'm just missing
something obvious here...
[ See
http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]