Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Returning an Object in a function

Posted on 2006-11-25
2
Medium Priority
?
203 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 1200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

926 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