473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

destructor

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 destructor:(twice).I
didnt run the program.What will be the output and why?

Thanks
Jun 27 '08 #1
5 1311
Ramkey pisze:
I was asked in a test
class A{
A ~A{
cout << "A's destructor";
}
};

int main(void){
A Aobj;
Aobj.~A;
}
it shouldn't be:
class A {
~A() {
cout << "A's destructor";
}

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

?? (missing "()" )

if so, there is error in "Aobj.~A();" , because A's destructor is private

if it would be public, this program should display:
"A's destructorA's destructor"
because of
1) "manually" run Aojb destructor
2) automatic destruction of Aobj
sorry for my english, Martin, PL
Jun 27 '08 #2
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 destructor:(twice).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.

Jun 27 '08 #3
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.
object is destroyed only once.
Jun 27 '08 #4
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.

Jun 27 '08 #5
Rolf Magnus pisze:
Dyziek wrote:
>Rolf Magnus pisze:
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.
maybe you are right
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object...
11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
11
by: AB | last post by:
Hi All, I've got an array of objects, during the execution of the program I'd like to assign a particular object to a certain element in the object array. The sample code's like this... class...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
8
by: gw7rib | last post by:
I've been bitten twice now by the same bug, and so I thought I would draw it to people's attention to try to save others the problems I've had. The bug arises when you copy code from a destructor...
7
by: sam | last post by:
Hi, See when i reading a sourcecode of a program, I read that the constructor is ordinary and after that the programmer has written virtual destructor for that constructor . Why we use the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.