1

I have in my project a couple of functions that create objects. For example, if you have a function that makes an object like this:

int& f()
{
    int *i = new int(5);
    return *i;
}

and then when you call that you do

int x = f();

or something like

std::cout << f();

what happens to the int that was created in the function? Is the reference handled like a normal variable or am I doing something terribly wrong? And if so, what is the correct way to make objects?

0

2 Answers 2

7

This is terribly wrong, indeed. The moment you forget about your reference is the moment you'll leak a resource. And you're doing just that with int x = f();.

Same with the second case, std::cout << f(); - don't use this at all if you aren't going to delete that dynamically allocated int.

int is also passed to std::basic_ostream::operator<< by value, a copy is made. This doesn't actually matter, no normal code would accept a reference and then call delete &ref;.

Here's a link for you: RAII.


I can't know what you want from your contrived example, but consider returning by value, or using smart pointers in case of polymorphism.

Sign up to request clarification or add additional context in comments.

5 Comments

So if I wanted to have a function that initialises an object then returns a reference to that object so you don't have to initialise two objects how would I go about that?
Do you mean to avoid a copy? RVO should be able to handle that.
@user2271513 Shouldn't you use a constructor to initialize an object?
In this case the constructor is called in the function and then you do some other stuff to the object and then return it. So I have a class with a member function that returns an object of that class based on the members of the class
@user2271513 Returning such an object by value is perfectly valid common practice. RVO will kick in. Don't optimize prematurely.
0

if you want to show that function delegates ownership of the created object with new you have to do it explicitly for example with std::unique_ptr. As you can see your function becomes self documented and you need not look into the body to understand who response for deleting:

std::unique_ptr<int> foo() {
   return std::make_unique<int>(5);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.