Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > destructor

Reply
Thread Tools

destructor

 
 
Ramkey
Guest
Posts: n/a
 
      06-23-2008
I was asked in a test
class A{
A ~A{
cout << "A's destructor";
}
};

int main(void){
A Aobj;
Aobj.~A;
}

What will be the output?

I wrote "A's destructor";
I think it may also be "A's destructor" "A's destructortwice).I
didnt run the program.What will be the output and why?

Thanks
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      06-23-2008
Ramkey wrote:

> I was asked in a test
> class A{
> A ~A{
> cout << "A's destructor";
> }
> };
>
> int main(void){
> A Aobj;
> Aobj.~A;
> }
>
> What will be the output?
>
> I wrote "A's destructor";
> I think it may also be "A's destructor" "A's destructortwice).I
> didnt run the program.What will be the output and why?


Ignoring the syntax errors in your code, the output is undefined. Attempting
to destroy an object twice has undefined behavior.

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      06-23-2008
Dyziek wrote:

> Rolf Magnus pisze:
>> Ramkey wrote:
>>
>> Ignoring the syntax errors in your code, the output is undefined.
>> Attempting to destroy an object twice has undefined behavior.
>>

>
> it is not destroying object twice, it is running its destructor twice.


That's what "destroying the object" means.

> object is destroyed only once.


No. Its memory is deallocated once.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 2 05-20-2008 02:20 AM
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 0 05-19-2008 07:05 PM
compiler generated destructor vs class implemented destructor arun C++ 2 06-13-2006 05:43 AM
Explicit destructor calls from inside base class destructor frs C++ 20 09-21-2005 09:22 AM
destructor needed Jacob Java 5 10-31-2003 05:01 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57