Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I read in an article that objects there is just during program implementation. Is it correct?
Posted
Comments
jim lahey 17-Mar-11 12:09pm    
pardon?
Sergey Alexandrovich Kryukov 17-Mar-11 14:44pm    
It does not sound like making any sense at all. Please also see my comment to Griff's answer.
--SA

1 solution

Yes.
Objects you create in your program are destroyed when they "go out of scope": I.e. the flow of control exits from the block in which the object was declared, be that an "if" block:
if(myCondition)
   {
   int myInt;
   ...
   } // myInt is out of scope here, and can be destroyed.
or a method:
C#
private void myMethod()
   {
   int myInt;
   ...
   } // myInt is out of scope here, and can be destroyed.

When your program ends, all objects are out of scope, and can be destroyed.

[edit]
Reading back on that, it implies that all objects are destroyed, which is not true: as long as there is a reference to an object, it is not eligible for destruction, so you can pass a reference to an object outside the scope in which it was created.
   Point myPoint;
   myMethod();
   ...
private void myMethod()
   {
   int myInt;
   Point p = new Point(100,100);
   myPoint = p;
   } // myInt is out of scope here and so is p, but myPoint is
     // referring to the Point object, so it will not be destroyed.
[/edit]
 
Share this answer
 
v2
Comments
Albin Abel 17-Mar-11 13:30pm    
Clear explanation on object scope. My 5
Sergey Alexandrovich Kryukov 17-Mar-11 14:44pm    
Griff, the problem is: exact platform/technology/language is not specified. Your answer is biased to managed platforms. You also should have mentioned that in managed platform the object is destroyed not when going out of scope and not even when the reference is lost, but at some later moment, depending on implementation of the GC.

I did not want to answer the Question, because it is not correct. The question mention "just during program implementation", which sounds like gibberish. It's good that you don't answer to this directly, but why answering at all? Just pointing out to absurdness of the question would be more appropriate.

--SA
OriginalGriff 17-Mar-11 15:27pm    
Hi SA - how are you doing?
The OP is clearly a beginner in OOP in particular and - it seems likely - programming in general. So the most obvious starting point is managed: either VB or C#. C++ tends to be rather too complex for the lecturers who get stuck with the beginners to cope with! :laugh:
So I answer the question based on assumptions from the OP implied question...
The problem with beginners is that they often don't understand the question themselves, so if you just go back with requests for more info they can get too confused and disheartened. If you go back with a relevant answer, it may be partially incorrect, but at least it gives them the feeling that we are trying to help.
We all had to start somewhere, and the first steps can be a bugger to get your head round!
Sergey Alexandrovich Kryukov 17-Mar-11 15:56pm    
Hi Griff. Thank you for sharing your ideas. I understand you. I think you should follow that picture of a beginner which is I think quite reasonable, but also take into account my notes to clarify your answer. Doesn't it sound right?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900