Solved

std::MAP for a string and object pointer

Posted on 2013-12-01
3
482 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
[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
3 Comments
 
LVL 31

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 34

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

688 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