| | |
need help with destructor(im a noob)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 48
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include<stdio.h> class q1 { int id; public: q1() { id = 1; printf("mkdef: %d\n", id); } q1(int start) { id = start + 1; printf("mknew: %d\n", id); } ~q1() { printf("rm: %d\n", id); } int something(int n) { printf("La-de-da:%d\n", id); return n*(id+3);} }; void foo(void) { q1 one(1); one.something(4); } int main() { q1 a; printf("%d\n", a.something(3)); foo(); q1 b(6); printf("Bye..Bye..."); return 0; }
here is the output log:
mkdef: 1
La-de-da:1
12
mknew: 2
La-de-da:2
rm: 2
mknew: 7
Bye..Bye...rm: 7
rm: 1
Hello.,
im doing this walkthrough and i understand everything except the last part when the destructor gets called last, and rm=1., And at what point are we going out of scope? when we return 0, or with printf statement "bye bye"? when we created a new class q1 b(6), and passed 6 to the function q1(int start){id=start + 1;.....}, so isnt rm should be 7? why it 1 when i compile????? Greatly appreciated
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
//create object called a
mkdef: 1
La-de-da:1
12
//create object called one
mknew: 2
La-de-da:2
//destroy object called two when closing } in foo() found
rm: 2
//create object called b
mknew: 7
Bye..Bye...
//find closing } for main() so destroy objects still in scope
//object b destroyed first since it is on top of stack of object variables, or at least that's how I understand it to work
rm: 7
//then destroy object a
rm: 1
I believe going out of scope occurs with closing brace creating the scope or with completion of loop if that's the definition of the scope.
mkdef: 1
La-de-da:1
12
//create object called one
mknew: 2
La-de-da:2
//destroy object called two when closing } in foo() found
rm: 2
//create object called b
mknew: 7
Bye..Bye...
//find closing } for main() so destroy objects still in scope
//object b destroyed first since it is on top of stack of object variables, or at least that's how I understand it to work
rm: 7
//then destroy object a
rm: 1
I believe going out of scope occurs with closing brace creating the scope or with completion of loop if that's the definition of the scope.
Klatu Barada Nikto
![]() |
Similar Threads
- Needed task help! (C++)
Other Threads in the C++ Forum
- Previous Thread: Classes and read and print functions
- Next Thread: problem solving (c-string) urgent
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






