Assignment Operator with auto_ptr

C

Chummaid

Hello All,
Can somebody please explain why the following is not supported ?

class A{};

A* a = new A();

auto_ptr <A> aptr = *a;// Not supported

// though this is supported.

auto_ptr <A> aptr(a);

Thanks in advance
CID
 
D

Donovan Rebbechi

Hello All,
Can somebody please explain why the following is not supported ?

class A{};

A* a = new A();

auto_ptr <A> aptr = *a;// Not supported

// though this is supported.

auto_ptr <A> aptr(a);

The constructor is declared explicit to disallow implicit conversions. This
is usually done when implicit conversion is potentially harmful/destructive.

In this case, the "conversion" results in the pointer getting deleted, so it
is potentially harmful, can lead to obscure bugs

Cheersm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top