Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep object and reference to an object created inside a function

Tags:

c++

I'm new to C++ so still learning about pointers and getting used to using them.

I have declared an object map in order to store objects that the user might access later on using the tag key.

map<string,MyObject*> myMap;

The objects I'm adding to myMap are created in a function called by my main

void myFunction(){
    ...
    MyObject obj1();
    MyObject* objPtr;
    objPtr = &obj1;
    myMap.insert(pair<string,MyObject*>("xxxx", objPtr));
    ...
    } 

When I execute this function the object pointer is perfectly inserted to myMap, but after the function execution I lose the reference to obj1 I guess because the pointer and the object were created locally inside the function, so I still have an element "xxx" in the map but with what I think is an empty reference after.

How can I keep the object and the reference globally? I wanted to create this object in the function since it has some variable parameters that needs to get from a user obj1(m,n). Thanks for any help.

like image 794
JLA Avatar asked Jan 26 '26 01:01

JLA


2 Answers

The code line

MyObject obj1;

defines a local variable which holds an instance of MyObject. Once the context of this variable is about to be left, the object will be destructed. Destruction of objects doesn't mean that pointers which point to them will be notified in any way (they are not set to 0 for example), accessing them is invalid.

You have basically the following options:

  • Create the object on the heap instead of locally. This is done using the new keyword. But then you have to delete the object at some point.

    MyObject *obj1 = new MyObject(arguments...);
    
  • Since this is often difficult to manage, smart pointers where invented. For example, since C++11, the shared_ptr was added (but it's also available in some compilers with the pre-release version C++0x). This type of smart pointer will automatically delete the object for you when the last reference will be destroyed.

    map<string,std::shared_ptr<MyObject>> myMap; // Note that I changed the type
    
    std::shared_ptr<MyObject> obj1 = std::make_shared<MyObject>(arguments...);
    myMap.insert(std::make_pair("xxxx", obj1));
    

    By the way, I used std::make_pair which will automatically deduce the template types for you.

  • Store values in the map. Don't use pointers at all. This is an option if your type can be copied (some types can't) as well as if some of your design decisions doesn't forbid you to do so. But in most cases it will be a good idea to store the values in the map. Please use references when fetching an element from the map which you want to modify:

    MyObject &obj = myMap["xxxx"]; // looking up the key
    obj.doSomeAction();            // modify it; no need to re-insert
    

    References behave similar to pointers (i.e. they only "point at" some instance), but there are some differences. For example, references always point to the same instance over their lifetime (you can't reassign references). Also, their syntax is equal to values, i.e. no -> required.

Personally, I'd prefer the third option if you don't need to put pointers to the same object at multiple places in your program. If this was the case, we speak of "shared" objects which is exactly the case where you want to use "shared" pointers. The first option is very low-level and should be considered "obsolete".

like image 115
leemes Avatar answered Jan 27 '26 15:01

leemes


Unless you have a strong reason to do otherwise, it might be simpler to store objects in the map:

map<string, MyObject> myMap;

Then:

void myFunction(){
  ...
  myMap.insert(std::make_pair("xxxx", MyObject()));
  ...
} 
like image 25
juanchopanza Avatar answered Jan 27 '26 14:01

juanchopanza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!