|
INTERVIEW QUESTIONS
PROGRAMMING LANGUAGES
C++
DETAILS
Question: What is your reaction to this line of code?
Answer: delete this; It is not a good programming Practice. A good programmer will insist that you should absolutely never use the statement if the class is to be used by other programmers and instantiated as static, extern, or automatic objects. That much should be obvious. The code has two built-in pitfalls. First, if it executes in a member function for an extern, static, or automatic object, the program will probably crash as soon as the delete statement executes. There is no portable way for an object to tell that it was instantiated on the heap, so the class cannot assert that its object is properly instantiated. Second, when an object commits suicide this way, the using program might not know about its demise. As far as the instantiating program is concerned, the object remains in scope and continues to exist even though the object did itself in. Subsequent dereferencing of the baller can and usually does lead to disaster. I think that the language rules should disallow the idiom, but that's another matter.
|
|
|
Category |
C++ Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.2) By 8464 users |
Added on |
7/16/2015 |
Views |
68860 |
Rate it! |
|
|
Question:
What is your reaction to this line of code?
Answer:
delete this; It is not a good programming Practice. A good programmer will insist that you should absolutely never use the statement if the class is to be used by other programmers and instantiated as static, extern, or automatic objects. That much should be obvious. The code has two built-in pitfalls. First, if it executes in a member function for an extern, static, or automatic object, the program will probably crash as soon as the delete statement executes. There is no portable way for an object to tell that it was instantiated on the heap, so the class cannot assert that its object is properly instantiated. Second, when an object commits suicide this way, the using program might not know about its demise. As far as the instantiating program is concerned, the object remains in scope and continues to exist even though the object did itself in. Subsequent dereferencing of the baller can and usually does lead to disaster. I think that the language rules should disallow the idiom, but that's another matter. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
When should you use multiple inheritance?
|
View Answer
|
|
What are the debugging methods you use when came across a problem?
|
View Answer
|
|
How the compilers arranges the various sections in the executable image?
|
View Answer
|
|
What are the differences between a C++ struct and C++ class?
|
View Answer
|
|
How do you know that your class needs a virtual destructor?
|
View Answer
|
|
What is the difference between new/delete and malloc/free?
|
View Answer
|
|
What happens when a function throws an exception that was not specified by an exception specification for this function?
|
View Answer
|
|
Can you think of a situation where your program would crash without reaching the breakball, which you set at the beginning of main()?
|
View Answer
|
|
What issue do auto_ptr objects address?
|
View Answer
|
|
Is there any problem with the following:
|
View Answer
|
|
Why do C++ compilers need name mangling?
|
View Answer
|
|
Is there anything you can do in C++ that you cannot do in C?
|
View Answer
|
|
What is problem with Runtime type identification?
|
View Answer
|
|
What is Pure Virtual Function? Why and when it is used ?
|
View Answer
|
|
What about Virtual Destructor?
|
View Answer
|
|
Is it possible to have Virtual Constructor? If yes, how? If not, Why not possible ?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All C++ Interview Questions & Answers - Exam Mode /
Learning Mode
|