GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Jun-2008, 21:24
warrener warrener is offline
New Member
 
Join Date: Mar 2008
Posts: 17
warrener is an unknown quantity at this point

Can you call a constructor within a destructor, or vise vesa?


folks, I came across questions about calling constructor or destructor.
I am curious whether you can:

Code:
1. call a constructor in a constructor 2. call a destructor in a constructor 3. call a constructor in a destructor 4. call a destructor in a destructor 5. call a constructor in a member function 6. call a destructor in a member function

So I wrote the following code and want to share with you what I got. Comments are welcomed! Please check if I miss anything.

1. call a constructor in a constructor
Code:
class A{ public: A(){ A(); x = 100; cout<<"A() "<<x<<endl; } int x; ~A(){ x = 0; cout<<"~A() "<<x<<endl; } }; int main() { A obj; return 0; }

Basically you are not actually calling the constructor, you are just generating a temp object.
If you use this->A() to call then you will get an error.
Code:
error: calling type `A' like a method

Also this brings a recursive calling. Soon you will use up memory and crash.

2. call a destructor in a constructor
Code:
class A{ public: A(){ this->~A(); x = 100; cout<<"A() "<<x<<endl; } int x; ~A(){ x = 0; cout<<"~A() "<<x<<endl; } }; int main() { A obj; return 0; }

You can't call ~A() without qualifying: this->~A(). At least I didn't figure out a way to avoid it.
Yes, you can call destructor in the class, and you can see it destructs the member variable x.

3. call a constructor in a destructor

Code:
class A{ public: A(){ x = 100; cout<<"A() "<<x<<endl; } int x; ~A(){ A(); x = 0; cout<<"~A() "<<x<<endl; } }; int main() { A obj; return 0; }
Again, you are not calling constructor, just a temp obj.

4. call a destructor in a destructor

Code:
class A{ public: A(){ x = 100; cout<<"A() "<<x<<endl; } int x; ~A(){ this->~A(); x = 0; cout<<"~A() "<<x<<endl; } }; int main() { A obj; return 0; }

This also gives you a dead loop.

5. call a constructor in a member function

Code:
class A{ public: A(){ x = 100; cout<<"A() "<<x<<endl; } int x; void foo() { cout<<"BEFORE foo"<<x<<endl; A(); cout<<"AFTER foo"<<x<<endl; } ~A(){ x = 0; cout<<"~A() "<<x<<endl; } }; int main() { A obj; obj.foo(); return 0; }

It works. just a temp obj.

6. call a destructor in a member function

Code:
class A{ public: A(){ x = 100; cout<<"A() "<<x<<endl; } int x; void foo() { cout<<"BEFORE foo"<<x<<endl; A(); cout<<"AFTER foo"<<x<<endl; } ~A(){ x = 0; cout<<"~A() "<<x<<endl; } }; int main() { A obj; obj.foo(); return 0; }

It works.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two bugs in my program that just won't go away. randomperson133 Assembly Language 1 23-Mar-2008 08:04
Will pay through Paypal if somebody helps me. paritoshcool Assembly Language 0 27-Nov-2007 23:27
Fortran problem... Justin Fox Miscellaneous Programming Forum 6 24-Oct-2006 16:30
how to get Call graph for applications, anybody knows bravetanveer C Programming Language 1 30-Sep-2005 00:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:09.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.