?
Solved

std::MAP for a string and object pointer

Posted on 2013-12-01
3
Medium Priority
?
490 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 1000 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 35

Assisted Solution

by:sarabande
sarabande earned 1000 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

Industry Leaders: 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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.
Suggested Courses

765 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