Thursday, July 9, 2009

In this c++ destructors are not getting initialized!!!!(destructors for obj2 and obj1 ar'nt invoked?

/*The life cycle of an object*/


#include%26lt;iostream%26gt;


#include%26lt;conio%26gt;


class Test


{


public:


Test()


{


cout%26lt;%26lt;"Constructor invoked"%26lt;%26lt;endl;


}


~Test()


{


cout%26lt;%26lt;"Destructor invoked"%26lt;%26lt;endl;


}


};


Test obj1;


int main()


{


cout%26lt;%26lt;"main() begins"%26lt;%26lt;endl;


Test obj2;


{


cout%26lt;%26lt;"Inner block begins"%26lt;%26lt;endl;


Test obj3;


cout%26lt;%26lt;"Inner block ends"%26lt;%26lt;endl;


}


cout%26lt;%26lt;"main() ends"%26lt;%26lt;endl;


getch();


return 0;


}





the output i get is





Constructor invoked


main() begins


Constructor invoked


Inner block begins


Constructor invoked


Inner block ends


Destructor invoked


main() ends





it should have been





Constructor invoked


main() begins


Constructor invoked


Inner block begins


Constructor invoked


Inner block ends


Destructor invoked


main() ends


Destructor invoked


Destructor invoked





plz help(destructor should automatically be initialized isn't it?)


i am using borland c++ builder 5.2 is it a factor??

In this c++ destructors are not getting initialized!!!!(destructors for obj2 and obj1 ar'nt invoked?
The following answer is absolutly correct ....





Constructor invoked


main() begins


Constructor invoked


Inner block begins


Constructor invoked


Inner block ends


Destructor invoked


main() ends


________________


|Destructor invoked |


|Destructor invoked |


|________________|





But u can't see the last 2 statements (showed in block)....here what actully happened is ...





The destructore for obj2 will be called after the "MAIN() FUNCTION" is terminated" (i.e. after the closing braces).


B'coz, obj2 is declaired in main() function, so its life is in the main() function's scope.


The destructor for obj1 will be called just b4 the "PROGARMM" is terminated (i.e. bust b4 the programm is removed from the memory).


B'coz, obj1 is a globle object of TEST class and its life is thruout the programm.





I dont know about the Borland Compiler, but in Turbo compiler you can se the output screen again after termination of the program using "ALT + F5" and doing this will show the real output...





u can contact me if u still have doubts... send me message.
Reply:I have altered some of your code so it will compile AND run. Forgive me as I have cut parts out regardless, I found you had multple problems with the code, such as how you were actually giving the function protyptes in the class actual code (only pass them parameters if needed, you do all the code you want the function to do in the defintion. A class can ONLY have one deconstrutor. Also if you are compling this in one file...as your code showed...then why did you not use namespace std? That was probably half of your errors right there you would have had to use std::cout %26lt;%26lt; "stuff on screen";





Good luck.


#include%26lt;iostream%26gt;


using namespace std;





//Define Class


class Test


{


public:


//Function Prototype


void TestFunction();


//Destructor


~Test();


};








int main()


{


//Declare object/variable


Test obj1;





cout%26lt;%26lt;"main() begins"%26lt;%26lt;endl;


//Function call


obj1.TestFunction();


cout%26lt;%26lt;"main() ends"%26lt;%26lt;endl;





system("pause");


return 0;


}


//Function Definitions


void Test::TestFunction()


{


cout%26lt;%26lt;"Inner block begins"%26lt;%26lt;endl;


}


Test::~Test()


{


}
Reply:Yeah, the last poster is right. I added this line at the top:





using namespace std;





and made other minor changes to get it going on unix.





I get the output you expected:





Constructor invoked


main() begins


Constructor invoked


Inner block begins


Constructor invoked


Inner block ends


Destructor invoked


main() ends


Destructor invoked


Destructor invoked








So, you might have an issue with text output. That doesn't explain why you get some lines printed out and not others, though. So, yeah, you might also have a flaky compiler, since the code works as expected with g++.


No comments:

Post a Comment