Solved

std::MAP for a string and object pointer

Posted on 2013-12-01
3
469 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 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 33

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

774 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