Solved

Returning an Object in a function

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

813 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

14 Experts available now in Live!

Get 1:1 Help Now