How do I return an auto_ptr of a base class using an auto_ptr of a derived class?

quant

I am working through some older code that must return an std::auto_ptr, with which I have relatively little experience. I have run into a situation like this:

// I need to populate this function
std::auto_ptr<Base> Func()
{
  std::auto_ptr<Derived> derivedPtr = new Derived;

  // now I want to return

  return derivedPtr; // error: conversion from std::auto_ptr<Derived> to std::auto_ptr<Base> is ambiguous
}

Do I need to release auto_ptr first? The really overly explicit way would be something like return static_cast<Base>(derivedPtr.release()) but I suspect this isn't necessary.

Tony Delroy

You can use...

return std::auto_ptr<Base>(derivedPtr);   // explicitly use constructor

...or...

return derivedPtr.operator std::auto_ptr<Base>();  // use cast/conversion operator

(The reason you can't just return derivedPtr is that the above are ambiguous candidates).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I return an auto_ptr of a base class using an auto_ptr of a derived class?

From Dev

Is it safe to initialize an auto_ptr with a pointer to a derived class?

From Dev

How do I add an auto_ptr to a vector

From Dev

Function taking a std::auto_ptr<Base> that can accept std::auto_ptr<Derived>

From Dev

Conflict in return type from base class with derived class using auto

From Dev

How to find the address of an auto_ptr

From Dev

How is std::auto_ptr initialized with a rvalue?

From Dev

How to find the address of an auto_ptr

From Dev

How to return a derived class using only code in the base class?

From Dev

Can I get away with putting auto_ptr in a STL container?

From Dev

Memory corruption error while using auto_ptr

From Dev

Why auto_ptr initialization using the assignment syntax is not allowed

From Dev

Why auto_ptr initialization using the assignment syntax is not allowed

From Dev

Base class unique_ptr to derived class shared_ptr

From Dev

Base class unique_ptr to derived class shared_ptr

From Dev

How to return derived type while operating over base and derived class?

From Dev

How to use a covariant return to retrieve a derived class from a base class?

From Dev

Inheritance : using base class or derived class to do stuff

From Dev

How to pass a derived class to a base class function and return the derived class c#

From Dev

Base class does not implement INotifyPropertyChange; how do I implement it in derived class?

From Dev

How do I override `toString` in my derived class and use the private instance variables from the base class?

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

How do I call a base class method from within the same overloaded derived class method in python?

From Dev

How do I call a derived class method from the base class constructor in C++?

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

Return value from Derived class to Base class

From Dev

"using" (or other mechanism) to swap in unique_ptr for auto_ptr in C++11?

From Dev

"using" (or other mechanism) to swap in unique_ptr for auto_ptr in C++11?

From Dev

How can I friend a derived class function in the base class?

Related Related

  1. 1

    How do I return an auto_ptr of a base class using an auto_ptr of a derived class?

  2. 2

    Is it safe to initialize an auto_ptr with a pointer to a derived class?

  3. 3

    How do I add an auto_ptr to a vector

  4. 4

    Function taking a std::auto_ptr<Base> that can accept std::auto_ptr<Derived>

  5. 5

    Conflict in return type from base class with derived class using auto

  6. 6

    How to find the address of an auto_ptr

  7. 7

    How is std::auto_ptr initialized with a rvalue?

  8. 8

    How to find the address of an auto_ptr

  9. 9

    How to return a derived class using only code in the base class?

  10. 10

    Can I get away with putting auto_ptr in a STL container?

  11. 11

    Memory corruption error while using auto_ptr

  12. 12

    Why auto_ptr initialization using the assignment syntax is not allowed

  13. 13

    Why auto_ptr initialization using the assignment syntax is not allowed

  14. 14

    Base class unique_ptr to derived class shared_ptr

  15. 15

    Base class unique_ptr to derived class shared_ptr

  16. 16

    How to return derived type while operating over base and derived class?

  17. 17

    How to use a covariant return to retrieve a derived class from a base class?

  18. 18

    Inheritance : using base class or derived class to do stuff

  19. 19

    How to pass a derived class to a base class function and return the derived class c#

  20. 20

    Base class does not implement INotifyPropertyChange; how do I implement it in derived class?

  21. 21

    How do I override `toString` in my derived class and use the private instance variables from the base class?

  22. 22

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  23. 23

    How do I call a base class method from within the same overloaded derived class method in python?

  24. 24

    How do I call a derived class method from the base class constructor in C++?

  25. 25

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  26. 26

    Return value from Derived class to Base class

  27. 27

    "using" (or other mechanism) to swap in unique_ptr for auto_ptr in C++11?

  28. 28

    "using" (or other mechanism) to swap in unique_ptr for auto_ptr in C++11?

  29. 29

    How can I friend a derived class function in the base class?

HotTag

Archive