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;

    // though this is supported.

    auto_ptr <A> aptr(a);

    Thanks in advance
    CID
    , May 12, 2005
    #1
    1. Advertising

  2. Kristo Guest

    wrote:
    > Hello All,
    > Can somebody please explain why the following is not supported ?
    >
    > class A{};
    >
    > A* a = new A();
    >
    > auto_ptr <A> aptr = *a;


    Contrary to what the thread subject says, this invokes auto_ptr's
    constructor. You're trying to pass the A object itself, not a pointer
    to it. This is the correct way to do it:

    > // though this is supported.
    >
    > auto_ptr <A> aptr(a);


    If you want to use the equals sign syntax, this line would be:
    auto_ptr<A> aptr = a;

    Kristo
    Kristo, May 12, 2005
    #2
    1. Advertising

  3. CID Guest

    Kristo wrote:
    > wrote:
    > > Hello All,
    > > Can somebody please explain why the following is not supported ?
    > >
    > > class A{};
    > >
    > > A* a = new A();
    > >
    > > auto_ptr <A> aptr = *a;

    >
    > Contrary to what the thread subject says, this invokes auto_ptr's
    > constructor. You're trying to pass the A object itself, not a

    pointer
    > to it. This is the correct way to do it:
    >
    > > // though this is supported.
    > >
    > > auto_ptr <A> aptr(a);

    >
    > If you want to use the equals sign syntax, this line would be:
    > auto_ptr<A> aptr = a;
    >
    > Kristo


    This will result in the following error in g++, which was my first
    doubt was.

    error: conversion from `A*' to non-scalar type
    `std::auto_ptr<A>' requested

    Thanks for the reply
    CID
    CID, May 12, 2005
    #3
  4. CID schrieb:
    > Kristo wrote:
    >
    >> wrote:
    >>
    >>>Hello All,
    >>>Can somebody please explain why the following is not supported ?
    >>>
    >>>class A{};
    >>>
    >>>A* a = new A();
    >>>
    >>>auto_ptr <A> aptr = *a;

    >>
    >>Contrary to what the thread subject says, this invokes auto_ptr's
    >>constructor. You're trying to pass the A object itself, not a

    >
    > pointer
    >
    >>to it. This is the correct way to do it:
    >>
    >>
    >>>// though this is supported.
    >>>
    >>>auto_ptr <A> aptr(a);

    >>
    >>If you want to use the equals sign syntax, this line would be:
    >> auto_ptr<A> aptr = a;
    >>
    >>Kristo

    >
    >
    > This will result in the following error in g++, which was my first
    > doubt was.
    >
    > error: conversion from `A*' to non-scalar type
    > `std::auto_ptr<A>' requested
    >



    that's because the constructor of auto_ptr is "explicit", and there's a
    obvious reason for it.
    use
    auto_ptr<A> aptr=auto_ptr<A>(a);
    instead
    Stefan Strasser, May 12, 2005
    #4
    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,552
    Dave Rahardja
    Jan 11, 2005
  2. Replies:
    1
    Views:
    398
    Donovan Rebbechi
    May 13, 2005
  3. Chris
    Replies:
    34
    Views:
    1,515
  4. Diego Martins
    Replies:
    10
    Views:
    2,176
    peter koch
    Nov 27, 2006
  5. Sousuke
    Replies:
    9
    Views:
    1,146
    Bart van Ingen Schenau
    Mar 16, 2010
Loading...

Share This Page