std::map copy constructor


I'm using MS Visual Studio 2003 .NET. I'm also using the builtin std-map class (#include <map>).

I have something like this code:
----- code -----
class Obj
  string val;
typedef std::map<std::string, Obj> str_to_obj;

str_to_obj return_function( )
  str_to_obj ret;
  ret["abc"] = Obj( ... );
  return ret;
----- code -----

Where the problem is, is that when returning a str_to_obj from a function, it doesn't seem to copy neither the keys, neither the values... The only thing which is copied seems to be the amount of items in there, but no reasonable data in there... [in this example: abc is totally gone, the string ain't even copied!].

I checked the file, and it seems that std::map doesn't have a copy constructor...

So could anyone tell me how I can get a str_to_obj with meaningfull data back out of this function?

Who is Participating?
>>I checked the file, and it seems that std::map doesn't have a copy constructor...

It has: map(const map& x);

But, does 'Obj' have a copy constructor and assignment operator?
half_life_foolAuthor Commented:
looks like i have a bugged version of std::map then??

file: Microsoft Visual Studio .NET 2003/Vc7/include/map
----- code ------
      explicit map(const key_compare& _Pred)
      map(const key_compare& _Pred, const allocator_type& _Al)
      template<class _Iter>
            map(_Iter _First, _Iter _Last)
      template<class _Iter>
            map(_Iter _First, _Iter _Last,
                  const key_compare& _Pred)
      template<class _Iter>
            map(_Iter _First, _Iter _Last,
                  const key_compare& _Pred, const allocator_type& _Al)
----- code ------

Obj is a struct, not a class sorry (if that would make any difference). And no, Obj has no copy nor assign operator.

But also the std::string (key) is not being copied over to the target map... So it wouldn't be only the Obj it's problem?
half_life_foolAuthor Commented:
Ok, never mind... I was looking at the wrong variables... It will probably be another bug in my program why the .find() function doesn't work properly.

Thanks for your help jkr!

----- code -----
// copyconst.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <string>
#include <map>

using namespace std;

struct Obj
  Obj( const string& val = "" )
    : abc(val) { }

  string abc;
typedef map<string, Obj> str_to_obj;

getstr( const char* c )
  char * b = new char[ strlen(c) + 1 ];
  strcpy(b, c);
  b[strlen(c)] = '\0';
  string retval(b);
  delete [] b;

  return retval;

#define TEST(a)  retVal[getstr(#a)] = Obj(#a)

return_function( )
  str_to_obj retVal;

  return retVal;

int _tmain(int argc, _TCHAR* argv[])
      str_to_obj test = return_function();

  for( str_to_obj::const_iterator ci = test.begin(); ci != test.end(); ci++ )
    cout << "str_to_obj[" << ci->first << "] = Obj(" << ci-> << ");" << endl;

----- code -----
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.