Assignment Operator with auto_ptr

Discussion in 'C++' started by Chummaid@gmail.com, May 12, 2005.

  1. Guest

    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
     
    , May 12, 2005
    #1
    1. Advertising

  2. On 2005-05-12, <> wrote:
    > 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
    --
    Donovan Rebbechi
    http://pegasus.rutgers.edu/~elflord/
     
    Donovan Rebbechi, May 13, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,582
    Dave Rahardja
    Jan 11, 2005
  2. Replies:
    3
    Views:
    484
    Stefan Strasser
    May 12, 2005
  3. Chris
    Replies:
    34
    Views:
    1,592
  4. Diego Martins
    Replies:
    10
    Views:
    2,216
    peter koch
    Nov 27, 2006
  5. Sousuke
    Replies:
    9
    Views:
    1,183
    Bart van Ingen Schenau
    Mar 16, 2010
Loading...

Share This Page