Sign in to follow this  
mylifemysoul

Code difference

Recommended Posts

mylifemysoul    100
Suddenly got confused by a piece of code. I saw it somewhere from an official Java website as samples. I wonder if the code will be running fine in c++?
SomeObject function()
{
   SomeObject so;
   so = do something here; 
   ....
   return so;
}

My confusion is that the so is a local variable. In java, when we define so like above, we actually define a reference for that Object. But, when the scope of the function ends, the local variable is no longer available. If the code is compiled in c++, will it compile fine? What if in c++, we define them as pointers like below, will it compile fine?
SomeObject* function()
{
   SomeObject* so = new() something here ;
   do something here; 
   ....
   return so;
}
delete the pointer ....

Share this post


Link to post
Share on other sites
visitor    643
The local object will be returned by value, which means that right before the function exits and the locals are destroyed, the return value is "copied out" using the copy constructor.

Note, the compiler may well elide the copying and destroying the temporary (most likely will be done if the function has a single return statement). This is known as the named return value optimization.

The second version is pretty much the same (technically, concerning the life-time of the thing you return). Again the caller receives a copy of the local pointer. However, the pointed-at object lives as long as it's not explicitly deleted. (Generally don't do it unless you have a good reason to avoid unnecessary memory management difficulties.)

Share this post


Link to post
Share on other sites
Decrius    100
First example will run fine since you don't return a reference but rather the whole object. The calling code will also create a SomeObject and be copy-constructed or assigned to the function() return.

The second example will also run fine, since an object on the heap is allocated (and a pointer to it locally on the stack). The pointer gets _copied_ to the caller code and the same allocated memory is still used. Yah, don't forget delete ;)

Share this post


Link to post
Share on other sites
ChaosEngine    5185
Except that you will forget to delete. This is not a slight on you. There are lots of ways you're delete won't get called (you forget it, an exception is thrown, a return statement is put in before the delete, etc.)

Far better, to let the object delete itself.

shared_ptr<SomeObject> function()
{
shared_ptr<SomeObject> so(new SomeObject());
so-> do something here;
....
return so;
}




edit: obviously, you can also go the copy constructor route. You need to decide which makes more sense for your application (value or reference semantics). But don't use a raw pointer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this