?
Solved

std::map copy constructor

Posted on 2006-07-08
3
Medium Priority
?
1,099 Views
Last Modified: 2013-12-14
Hi,


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?


Thanks!
0
Comment
Question by:half_life_fool
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 17065576
>>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?
0
 

Author Comment

by:half_life_fool
ID: 17065602
looks like i have a bugged version of std::map then??

file: Microsoft Visual Studio .NET 2003/Vc7/include/map
----- code ------
      map()
      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?
0
 

Author Comment

by:half_life_fool
ID: 17065646
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;


string
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)


str_to_obj
return_function( )
{
  str_to_obj retVal;
  TEST(abc1);
  TEST(abc2);
  TEST(abc3);
  TEST(abc4);
  TEST(abc5);

  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->second.abc << ");" << endl;
  }
}

----- code -----
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question