answersLogoWhite

0

Not to be pedantic, but you cannot call a destructor explicitly. Destructors are implicitly called when an object falls from scope or when you delete a pointer to an object.

Any object created dynamically (with the new keyword) must be deleted as soon as you are finished with it, and before the pointer falls from scope. In this sense, you are explicitly calling the object's destructor, however it's really being called implicitly by you deleting the pointer. It's also good practice to explicitly NULL your pointer immediately after deleting the object it pointed to.

An object reference is destroyed automatically when the reference falls from scope. If you have a pointer to that reference, do not delete the pointer, but do assign it to NULL as soon as possible to prevent any access to the deleted object. If you do delete a pointer to a reference that's still in scope, you will render the reference NULL and a NULL reference will render your program invalid.

User Avatar

Wiki User

12y ago

What else can I help you with?

Continue Learning about Engineering

What is issue in throwing an exception in a destructor?

If a destructor throws an exception, the instance is left in an invalid state. When an exception is thrown, the destructor automatically terminates at the point of the throw, unwinding the call stack until an exception handler is found (if one is provided). However, any resources yet to be released by the destructor, including all the instance's base classes, cannot be destroyed. When writing your own destructors, it is important to never throw an exception. If an exception could be thrown from within your destructor, you must catch it and handle it within the same destructor -- you must not rethrow the exception.


When destructor can be virtual in c plus plus?

A class constructor initialises object member variables, ensuring the object is in a valid state from the point of instantiation. The destructor resets those same variables when the object falls from scope. Typically, the destructor is only required to release any memory allocated to any internal class pointers.


What do you call a person who is addicted to computer programming?

One thing that you can call a person that is addicted to computer programming is a computer nerd. A computer nerd is always on the computer.


What do you call programming languages that require you to declare the type of each variable?

Statically typed languages.


What object base programming?

Object base programming is the one that in programming code only object is used to call method(function, procedure whatever you call it). Some small amount of primitive data type is also used. Take a dog as object. Dog has name, age. if you want to find out the name and age of the dog, you need to make a method called public getName(){retrun name} etc. and call the same method. while you call the method, you must use the dog object. In this type of programming object carries its properties with it. that is it my frind. good luck

Related Questions

Why constructor and destructor cannot be made static?

The term "destructor" made me believe this question is related to .Net languages. A destructor is to destroy an instance of object. If it is available at static/class level, what is it going to destroy? The entire class, so the class no longer available? Thus, semantically, destructor should be an instance method. Constructor is on the opposite end of the life cycle of an instance. However, in .NET, a static constructor is allowed. Personally, I call this static constructor as a class initialization method. This method will be invoked by the .net framework only once when the class is loaded into the application domain. With the similar concept, there should be a "finalizer" of the class when the class is unloaded out of the application domain. But wait, does a class ever go out of the application domain once it's loaded? Yes, only at the termination of the application! Currently a class cannot be unloaded explicitly in codes and thus no point to have a static finalizer.


When is an object created and what is its lifetime?

until the object goes out of scope. or until u call a destructor


What is issue in throwing an exception in a destructor?

If a destructor throws an exception, the instance is left in an invalid state. When an exception is thrown, the destructor automatically terminates at the point of the throw, unwinding the call stack until an exception handler is found (if one is provided). However, any resources yet to be released by the destructor, including all the instance's base classes, cannot be destroyed. When writing your own destructors, it is important to never throw an exception. If an exception could be thrown from within your destructor, you must catch it and handle it within the same destructor -- you must not rethrow the exception.


Difference between super and this in java?

The keyword super is used to explicitly call methods/values from the parent class The keyword this is used to explicitly call methods/values from the current class


When destructor can be virtual in c plus plus?

A class constructor initialises object member variables, ensuring the object is in a valid state from the point of instantiation. The destructor resets those same variables when the object falls from scope. Typically, the destructor is only required to release any memory allocated to any internal class pointers.


What is embry call favorite colour?

In the Twilight series, Embry Call's favorite color is not explicitly stated.


How do explicitly invoke superclass constructor from a subclass?

Using the super keyword. If you call super() from within your constructor, it will explicitly invoke the superclass version of the constructor.


You cannot throw an exception from a destructor?

Sure you can, but it's never a good idea. You must never allow an exception to escape from a destructor. To do so would terminate the destructor prematurely, unwinding the call stack in search of an exception handler. This means that any remaining resources consumed by the instance, including its base classes, would be left in an invalid state with no possible way to recover those resources besides terminating the program. Any object that has the potential to throw an exception during its own destruction should always be treated with suspicion; it is a major design flaw.


What do you call a person who is addicted to computer programming?

One thing that you can call a person that is addicted to computer programming is a computer nerd. A computer nerd is always on the computer.


What programming language was Call of Duty 5 written in?

C++


What are the limitation of structured programming?

By itself, structured programming does not support the notion of a function call. This is achieved through an extension of structured programming known as procedural programming. Object-oriented programming extends procedural programming such that data and the functions that operate upon the data can be encapsulated within an object.


What is the difference between call by name and call by value in programming languages?

In programming languages, call by value passes the value of a variable to a function, while call by name passes the name of the variable. Call by value evaluates the value before passing it, while call by name evaluates the value when it is used in the function.