Jump to content
  • Advertisement
Sign in to follow this  
sheep19

Do objects go out of scope?

This topic is 3965 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

for( int i = 0; i < farmSize; i++ )
	{
		string tempName;
		cout << "Enter name for animal " << i + 1 << " : ";
		cin >> tempName;
		farm1.Add( Critter( tempName) );
	}
Let's say I have two objects. One named Farm and one named Critter. Farm has a member function that takes a Critter a parameter and adds it into a vector of Critters. In the above code, do my objects go out of scope when the loop ends? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
The Critter temporary you create will be destroyed. Whatever happens from there depends on how your Add() member function is defined.

Share this post


Link to post
Share on other sites
The scope of the Critter object is a little tricky. I assume you have some kind of collection, such as a vector<Critter> object named critters? If that's so, you're passing a temporary object, its scope ends as soon as that statement ends. But, if you do something like critters.push_back(critter); in Farm::Add then the copy constructor will be called on the critter passed to Add. This new copy will have the scope of the Farm object itself.

Share this post


Link to post
Share on other sites
This is the add member function


void Critter::Add( const Critter& aCritter )
{
m_Critters.push_back( aCritter );
}



So, does it go out of scope?

Quote:
If that's so, you're passing a temporary object, its scope ends as soon as that statement ends. But, if you do something like critters.push_back(critter); in Farm::Add then the copy constructor will be called on the critter passed to Add. This new copy will have the scope of the Farm object itself.


What about if I didnt pass by reference, and made a copy of the object? When the object in main will be destroyed, the copy will remain. Am I thinking correctly?

If no, how can I do it?

Share this post


Link to post
Share on other sites
Quote:
Original post by sheep19
What about if I didnt pass by reference, and made a copy of the object? When the object in main will be destroyed, the copy will remain. Am I thinking correctly?
Correct.

Share this post


Link to post
Share on other sites
All you're doing at the moment is making 1 copy, which is not a big deal unless it's a massive structure or particularly slow to copy in some way. If you really want to minimise copying then you can start working with pointers to objects instead, but that comes with its own problems.

Share this post


Link to post
Share on other sites
Quote:
Original post by sheep19
What about if I didnt pass by reference, and made a copy of the object? When the object in main will be destroyed, the copy will remain. Am I thinking correctly?


Are you talking about the copy made implicitly by passing by value? That would get destructed when the Add() function ends, while the temporary within main() gets destructed at the end of the statement.

But neither of those matter, because your vector will contain yet another copy. And there's no way around making *that* copy, because that's how the vector works. (You can't just directly "construct something into" the vector, because it doesn't know how to call the constructor for your object.) And the vector is managing a dynamic allocation; its elements last as long as the vector does (unless you explicitly erase them or something).

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
destructed

<nitpick>Destroyed. Destructed isn't a word. The term destructor comes from the word destruction which is the subject noun form for the verb destroy.</nitpick>

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!