Question: 3. (The Smart Pointer std::auto_ptr) This pointer type is deprecated in C++11. Consider the following code: using std::auto_ptr; // Define auto_ptr pointers instead of raw

3. (The Smart Pointer std::auto_ptr)

This pointer type is deprecated in C++11. Consider the following code:

using std::auto_ptr;

// Define auto_ptr pointers instead of raw pointers std::auto_ptr d(new double (1.0));.

// Dereference *d = 2.0;
// Change ownership of auto_ptr // (after assignment, d is undefined)
auto_ptr d2 = d;
*d2 = 3.0;
cout << "Auto values: " << *d.get() << ", " << *d2.get();
Answer the following questions:

a) Type the above code and run it. Why does it give a run-time error?

b) Port the code to code that uses std::unique_ptr. Run and test the new code.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting For Financial Instruments Questions!