auto_ptr<Derived> to auto_ptr<Base>

Discussion in 'C++' started by Siemel Naran, Jan 10, 2005.

  1. Siemel Naran

    Siemel Naran Guest

    This code fails compile

    std::auto_ptr<Base> f() {
    std::auto_ptr<Derived> out(new Derived());
    return out;
    }

    There is ambiguity between a templated constructor and templated operator
    conversion, according to my compiler. Seems there are too many constructors
    and operator conversions. But this code works:

    int main() {
    std::auto_ptr<Derived> derived(new Derived());
    std::auto_ptr<Base> base(derived);
    return out;
    }

    Is my compiler broken? Why the difference between the two snippets of code.
    Siemel Naran, Jan 10, 2005
    #1
    1. Advertising

  2. Siemel Naran

    Howard Guest

    "Siemel Naran" <> wrote in message
    news:_vsEd.94345$...
    > This code fails compile
    >
    > std::auto_ptr<Base> f() {
    > std::auto_ptr<Derived> out(new Derived());
    > return out;
    > }
    >
    > There is ambiguity between a templated constructor and templated operator
    > conversion, according to my compiler. Seems there are too many
    > constructors
    > and operator conversions. But this code works:
    >
    > int main() {


    main??? Do you mean "std::auto_ptr<Base> f()"?

    > std::auto_ptr<Derived> derived(new Derived());
    > std::auto_ptr<Base> base(derived);
    > return out;
    > }
    >
    > Is my compiler broken? Why the difference between the two snippets of
    > code.
    >
    >
    >


    What exactly are you trying to do? Looking at this code and the code in
    your last post, it looks like you're trying to create a pointer to a Base
    class object, initializing it with a Derived class object. But the code
    above makes no sense.

    Your previous code looked better to me, as far as creating the object. But
    then you tried to call a function in that object which you made protected.
    You can't do that, except from inside one of its own member functions (or
    one of its own descendants).

    So now it looks like to get around that problem, you're trying to force a
    Derived object into a Base object.

    I'd go back to your earlier design (assuming I'm understanding it
    correctly). But I'd make the setvariable function public instead of
    protected.

    All this is based on speculation, however, since you're providing way too
    little information. We need the real code (or at least a much more complete
    sample of it), and exactly what it is you're trying to accomplish with the
    code, before we could really help, I think.

    -Howard
    Howard, Jan 10, 2005
    #2
    1. Advertising

  3. Siemel Naran wrote:
    > This code fails compile
    >
    > std::auto_ptr<Base> f() {
    > std::auto_ptr<Derived> out(new Derived());
    > return out;
    > }
    >
    > There is ambiguity between a templated constructor and templated operator
    > conversion, according to my compiler. Seems there are too many constructors
    > and operator conversions. But this code works:
    >
    > int main() {
    > std::auto_ptr<Derived> derived(new Derived());
    > std::auto_ptr<Base> base(derived);
    > return out;
    > }
    >
    > Is my compiler broken? Why the difference between the two snippets of code.


    This works on my compiler (MSVC.net 2003)
    ---
    #include <memory>

    using std::auto_ptr;

    class Base {};
    class Derived: public Base {};

    auto_ptr<Base> f()
    {
    auto_ptr<Derived> d(new Derived());
    return d;
    }
    ---

    -dr
    Dave Rahardja, Jan 11, 2005
    #3
    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. qazmlp
    Replies:
    1
    Views:
    567
    qazmlp
    Apr 10, 2005
  2. Stephan Hoffmann
    Replies:
    2
    Views:
    620
    Stephan Hoffmann
    Dec 11, 2005
  3. Replies:
    4
    Views:
    409
    Alf P. Steinbach
    May 23, 2007
  4. Replies:
    1
    Views:
    394
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    386
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page