Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return references to object created inside a method

I am reasoning about the best approach to return references to objects created inside a method, like in the following situation:

class A{
public:
    A(){}
    ~A(){}
};

class Foo{
public:
    Foo(){}
    ~Foo(){}
    A& create(int random_arg){
         // create object A and return its reference
    }
};

void other_method(){
     Foo f;
     A a = f.create();
     // do stuff with a
{

I have considered three possible solutions:

  1. create a raw pointer and return a reference, but this is bad because there is no guarantee that the object will be properly deleted:

    A& create(int random_arg){
         A* a = new A();
         return *a;
    }
    
  2. create a shared_ptr and return the shared_ptr by value. In this way the shared_ptr will take care of the object deletion:

    shared_ptr<A> create(int random_arg){
         boost::shared_ptr<A> a_ptr(new A());
         return a_ptr;
    }
    
  3. create a shared_ptr and return a reference:

    A& create(int random_arg){
         boost::shared_ptr<A> a_ptr(new A());
         return *a_ptr;
    }
    

The second solution seems to be the most used, but in this way I have to spread shared_ptr in the application and I would prefer to have references, or better const references.

What do you think is the best way to handle this situation? Are there other possibilities I have not considered?

like image 207
gcswoosh Avatar asked Mar 12 '15 13:03

gcswoosh


People also ask

Can a method return a reference to an object?

1) Yes, it returns a reference to the object. 2) If the method is private, then it can only be called from within the class itself.

Can a reference variable be returned from a method?

A C++ function can return a reference in a similar way as it returns a pointer. When returning a reference, be careful that the object being referred to does not go out of scope. So it is not legal to return a reference to local var. But you can always return a reference on a static variable.

What does it mean to return a reference to an object?

It means you return by reference, which is, at least in this case, probably not desired. It basically means the returned value is an alias to whatever you returned from the function. Unless it's a persistent object it's illegal.


2 Answers

Don't do this. You're most likely to have a dangling reference.

Just return the instance of A by value instead.

It's likely that the compiler will elide the implied object copy. But you can guarantee that an object copy will not be made by writing a move constructor for A.

like image 50
Bathsheba Avatar answered Sep 21 '22 01:09

Bathsheba


To complete Bathsheba's answer:

There is only one case where you might want to do something like that, that is if you are returning an instance to a singleton. In this case such instance will be a static object:

A& getTheOnlyInstanceOfA()
{
   static A onlyInstOfA;
   return onlyInstOfA; 
}
like image 25
sbabbi Avatar answered Sep 19 '22 01:09

sbabbi