Sign in to follow this  
Sean_Seanston

Objects and Function Scope

Recommended Posts

Just a quick question about something I'm not sure of. A is to contain an object of type B. A function does something like this:
void function()
{
B objectB;

A objectA;
A.init( objectB );

Vector.push_back( A );
}

Where A.init( B ); should set A's B object to objectB. Now, since objectB was declared inside a function that quickly goes out of scope, will there be problems with this? Say A has a pointer to a B. If that's set to point to objectB by init(), will that mean the pointer will point somewhere erroneous after the function returns or will it be ok? I have a feeling it'll go wrong but I don't have much experience in this area. As for not using a pointer, how would that work? Am I right in thinking you could just have a copy constructor and then just A.B = objectB? Those are the 2 basic options, right? Copying one into the other or using a pointer.

Share this post


Link to post
Share on other sites
Yeah, it depends on what "init( objectB );" is/does exactly.

class A
{
void init( B& b )
{
p = &b;//store the address of b
}
B* p;
}
^^ With this code, you're storing the address of the variable which is passed in. In your example, that variable will be destroyed when the function exits, so your pointer will be pointing at garbage, which is very very bad.
class A
{
void init( const B& b )
{
var = b;//store a copy of b
}
B var;
}
^^ With this code, you're storing a copy of the variable that is passed in, so you it doesn't matter that the original is destroyed.

Share this post


Link to post
Share on other sites
Even though the code above wouldn't compile...

You are not passing a pointer, but passing by value, which creates a copy.

And if you were to pass a pointer, then it would be to stack allocated instance, which is invalid after function returns.

In addition, when you put A into vector, a copy of A is made as well, which also makes another copy of B owned by A.

There are no pointers in example given.

Share this post


Link to post
Share on other sites
Right, that's more or less what I thought.

So if I wanted a function to instantiate an object and then have that object's values assigned to another that would survive after the function returns... I'd need a copy constructor, right?

Actually, I've been reading up about copy constructors... am I right in thinking that if I just want an object to have the same values and same pointers pointing to the same place as another, I can just use = and the default copy constructor will be fine?

Quote:
Original post by Antheus
In addition, when you put A into vector, a copy of A is made as well, which also makes another copy of B owned by A.


Yeah, I figured A was copied as well but I wasn't completely sure either, thanks.

Share this post


Link to post
Share on other sites
The basic rule is, if you define one of the following, you must probably define all of those:

- copy constructor
- assignment operator
- destructor

For example, if your destructor says delete p, and you don't provide your own copy constructor and assignment operator, you have a problem.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this