4

I encountered such code.

MyClass MyClass::get_information (const some_datastructure *record)
{
    auto_ptr<MyClass > variable (new MyClass ());

    variable ->set_article_id(record->article_id);

    return *variable.get();
}

I understand this returns a (copy?) of object of type MyClass. Initially, I thought it was returning auto_ptr object which didn't make sense to me (?) since I thought auto_ptr object get destroyed when it goes out of scope.

Anyway, is the above code Ok? Does object *variable.get() exist when/after the function returns?

pseudonym_127
  • 357
  • 7
  • 16
  • As a general rule, I'd consider any code using `auto_ptr` to be too fragile to consider "ok". But I hate that class with a bit too much passion. By any chance can you use `std::unique_ptr`? If so, run don't walk... – Yakk - Adam Nevraumont Jun 13 '13 at 15:26

2 Answers2

4

since it is returned by value, yes, the object is fine, although I don't understand the use of pointer or heap allocation for the matter... Would be simpler with a regular variable:

MyClass var;
var.set_article_id(record->article_id);
return var;
David G
  • 94,763
  • 41
  • 167
  • 253
Julien Lopez
  • 1,021
  • 6
  • 14
  • Hi, in that sense I can return any local variable (_given_ it is a class object) from a function right? Since a copy of it will be made? – pseudonym_127 Jun 13 '13 at 14:59
  • yeah, as long as you return by value, you can return pretty much anything. Although it's a bit tricky if the object is expensive to copy, like a big class, or a vector with lots of elements... – Julien Lopez Jun 13 '13 at 15:00
  • Yes but with C++0x rvalue references were introduced. This makes that things like this `MyClass localVar = FuncReturningValue();` will work with only one actual copy. – Sebastian Hoffmann Jun 13 '13 at 15:00
  • @Julien Lopez: But as far as I know e.g., I cant return an `int` from a function right? If it is local? as it will go out of scope. – pseudonym_127 Jun 13 '13 at 15:02
  • you can, it will actually create a copy of it (if its a complex type) – Sebastian Hoffmann Jun 13 '13 at 15:03
  • @Paranaix yup, even better :p – Julien Lopez Jun 13 '13 at 15:03
  • I think I am confusing here: I think what I can't do is return a `pointer` to a variable which has been locally defined inside the function right? Otherwise I think, by value, I can return anything – pseudonym_127 Jun 13 '13 at 15:04
  • @pseudonym_127 no, it'll be copied as well, there's nothing wrong with that, you can return an int without problem. – Julien Lopez Jun 13 '13 at 15:05
  • Yes thats right, the pointer would point to an invalid location (to the stackframe of the called function in fact, but that one is deallocated after the call ;)). e.g: things like this `MyClass local; return &local` (return type `MyClass*`) would be invalid, whereas `MyClass local; return local;` (return type `MyClass`) are just fine. – Sebastian Hoffmann Jun 13 '13 at 15:05
  • yup, a pointer or a reference will be rendered invalid as you quit the scope of the function, but a return by value will make a copy (or it'll be optimized away, but still works) and there's nothing to worry about except maybe performances in certain cases. – Julien Lopez Jun 13 '13 at 15:06
  • Btw, you might want to read: http://stackoverflow.com/questions/3106110/what-are-move-semantics if you want to know the exact details of how it works in c++0x now, but im afraid that this might be too advanced for you atm. I even hear expirienced programmers complaining about the complexity and the confusion the high use of implication this topic arises. – Sebastian Hoffmann Jun 13 '13 at 15:12
  • Ah forget my comment, didnt read that "allocated inside the function". You should never hold pointer to data only available in the scope of a function, never! Thats bad practices – Sebastian Hoffmann Jun 13 '13 at 15:14
3

Yes it does

Actually it creates a temporary rvalue of the underlying object of the pointer, in fact a copy. Notice that the return type is not MyClass* but MyClass. Thats why a copy is returned. *variable.get() also yields a rvalue.

Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77