Solved

Returning an Object in a function

Posted on 2006-11-25
2
196 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
  • 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now