This rule is Obsolete | |
Synopsis: | A function should not return a raw pointer |
Language: | C++ |
Severity Level: | 4 |
Category: | Object Oriented Programming |
Description: |
Instead of returning a raw pointer, one better returns for instance a unique_ptr. In that case the ownership is better defined. Wrong example: // create a Thing and return a pointer to it: Thing* create_Thing() { Thing* local_ptr(new Thing); return local_ptr; } void foo() { Thing* p1(create_Thing()); // it is unclear who owns the Thing ... } Right example: // create a Thing and return a unique_ptr to it: unique_ptr<Thing> create_Thing() { unique_ptr<Thing> local_ptr(new Thing); return local_ptr; // local_ptr will surrender ownership } void foo() { unique_ptr<Thing> p1(create_Thing()); // p1 owns the Thing now ... } |