(Local Variable Destructors) Write a program illustrating that all destructors for objects constructed in a block are called before an exception is thrown from that block.
What will be an ideal response?
```
// Class TestObject definition.
class TestObject
{
public:
TestObject( int ); // constructor takes int parameter
~TestObject(); // destructor
private:
int value;
}; // end class TestObject
```
```
// Class TestObject member function definition.
#include
#include "TestObject.h"
using namespace std;
// constructor takes int parameter
TestObject::TestObject( int val ) : value( val )
{
cout << "TestObject " << value << " constructor\n";
} // end TestObject constructor
// destructor
TestObject::~TestObject()
{
cout << "TestObject " << value << " destructor\n";
} // end TestObject destructor
```
```
#include
#include
using namespace std;
#include "TestObject.h"
int main()
{
try // create objects and throw exception
{
// create three TestObjects
TestObject a( 1 );
TestObject b( 2 );
TestObject c( 3 );
cout << '\n';
// throw an exception to show that all three Objects created above
// will have their destructors called before the block expires
throw runtime_error( "This is a test exception" );
} // end try
// catch the Error
catch ( runtime_error &exception )
{
cerr << exception.what() << "\n";
} // end catch
return 0;
} // end main
```
TestObject 1 constructor
TestObject 2 constructor
TestObject 3 constructor
TestObject 3 destructor
TestObject 2 destructor
TestObject 1 destructor
This is a test exception
You might also like to view...
Which of the following represents the Copy button?
A.
B.
C.
D.
During the execution of a legal warrant to search a home for evidence related to drug charges, officers see a video playing on the DVD player that is clearly child pornography. They confiscate the DVD as evidence.
a. It is not admissible in court because it was not defined in the warrant. b. The plain view doctrine makes that evidence admissible. c. The officers must wait for a new warrant before confiscating the DVD. d. This is a case of probable cause. No warrant is necessary at all.
A hard boot takes more time than a soft boot.
Answer the following statement true (T) or false (F)
If you have direct knowledge of child exploitation, you should
A) Notify your local police B) Notify the FBI C) Notify the National Center for Exploited and Missing Children D) All these answers are true.