Solved

std::MAP for a string and object pointer

Posted on 2013-12-01
3
464 Views
Last Modified: 2013-12-07
map<string, Object*> objectMap;

Object *pObject1;
Object *pObject2;

pObjet1 = new Object();

objectMap["Object1"] = pObject1;

pObject2 = objectMap["Object1"] ;

For some reason the pObject1 and pObject2 not point to the same object? Why that happen? How can I assign the pObject1 address into the objectMap["Object1"] so objectMap["Object1"] will return the same address (i.e. pObject1) rather a different address (pObject2)
0
Comment
Question by:tommym121
3 Comments
 
LVL 30

Accepted Solution

by:
Zoppo earned 250 total points
ID: 39689247
Hi tommym121,

ist this the real code you use?

In the fourth line, where you create the instance, you assign it to an undeclared pObjet1 instead of pObject1 - I guess that's a typo, but if not this might be the cause of your problem.

When I replace the pObjet1 with pObjetC1 the code works as expected, i.e. when I add
      std::cout << std::hex << "pObject1 = 0x" << pObject1 << std::endl << "pObject2 = 0x" << pObject2 << std::endl;
afterward the output I recieve is something like:
  pObject1 = 0x006B7C08
  pObject2 = 0x006B7C08

ZOPPO
0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 250 total points
ID: 39689499
I think ZOPPO is right.

if the map is the only container for "Object", you should turn the pointer type to Object type.

you could add and reference  "objects" then by

objectMap["Object1"] = Object();
Object & object2 = objectMap["Object1"] ;

Open in new window


if the Object class is intended as base class for virtual use, you have to use pointer type. however, it often makes sense to have a vector container as main container which is responsible for deleting the pointers, while the map only provides fast access to the data by key.

std::vector<Object*> objectArr;
std::vector<std::string, Object*> objectMap;
...
objectArr.push_back(new Object("Name"));
objectMap["Name"] = objectArr.back();
...
// finally delete all pointers
while (objectArr.begin() != objectArr.end())
{
      delete objectArr.back();
      objectArr.pop_back();
}
objectMap.clear(); // pointers are already deleted.

Open in new window


Sara
0
 

Author Closing Comment

by:tommym121
ID: 39703277
Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now