Solved

std::MAP for a string and object pointer

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

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

Suggested Solutions

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 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