Wie kann ich testen, ob ein auto_ptr gültig ist?
-
Ich möchte testen, ob ein auto_ptr noch gültig ist (z.B. nach Zuweisung wird er ja auf NULL gesetzt)? Ein Vergleich mit NULL geht nicht, weil == oder != für auto_ptr nicht definiert ist. Mit Prüfen auf typeid geht es auch nicht, weil diese nach dem "delete" noch gleich ist.
-
if(ap.get() != NULL)
-
if (ap) // bzw. if (!ap)oder?
-
Bashar schrieb:
if (ap) // bzw. if (!ap)oder?
Nein.

-
Josuttis:
T* auto_ptr::get() const throw()
• Returns the address of the object that the auto_ptr owns (if any).
• Returns the null pointer if the auto_ptr does not own an object.
• This call does not change the ownership. Thus, on exit the auto_ptr still owns the object that it owned on entry (if any).