Solved

Returning an Object in a function

Posted on 2006-11-25
2
201 Views
Last Modified: 2010-04-01
Hi Experts,

I have the following.  In getB( ) function when I return the Object b, would I have any problems.  I think If I do " B b = new B( ); " and returning &b would cause problem as the function returns, b goes out of scope.  Is it true even in my case?
 

class A
{

};

class B
{

};

class C
{
public:
     C( A& aa);
     B& getB( );  
private:
    A * m_aa;
};

B& C::getB( )
{
     B b;
    //do some initialization of B here
    return b;
}
0
Comment
Question by:ambuli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 18012256
There are several options depending on what members B contains. If they are just plain data, you could

B C::getB( ) // return an instance, not a reference
{
     B b;
    //do some initialization of B here
    return b;
}

void C::getB(B& r )
{
     B b;
    //do some initialization of B here
    r = b;
}

The same applies if you have more complex members, however you night need a copy constructor (http://en.wikipedia.org/wiki/Copy_constructor) and assignement operator (http://en.wikipedia.org/wiki/Assignment_%28computer_science%29). See also http://en.wikipedia.org/wiki/Object_copy
0
 
LVL 86

Expert Comment

by:jkr
ID: 18012280
Oh, and you are right

B* C::getB( )
{
     B* b = new B;
    //do some initialization of B here
    return b;
}

would also be an option, yet that would require more attention about deleting the objects. Also

B& C::getB( )
{
     B b;
    //do some initialization of B here
    return b;
}

will *not* work, since the reference returned would be invalid as soon as the function exits.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question