A
Alf P. Steinbach
Code as follows:
#include <iostream> // std::cout
#include <string> // std::string
#include <memory> // std::auto_ptr
class Window
{
public:
typedef std::auto_ptr<Window> AutoPtr;
virtual ~Window() {}
static AutoPtr newObject() { return AutoPtr( new Window ); }
}; // class Window
class Button: public Window
{
public:
typedef std::auto_ptr<Button> AutoPtr;
static AutoPtr newObject() { return AutoPtr( new Button ); }
}; // class Button
int main()
{
Window::AutoPtr pWindow( Window::newObject() );
Window::AutoPtr pButton( Button::newObject() ); // <--
std::cout << "Onky donk" << std::endl;
}
The line marked with an arrow attempts to convert a Button::AutoPtr to
a Window::AutoPtr.
I had the impression that this should work fine, but compiled with VC
7.1 it just enters an infinite recursion in 'auto_ptr<Button>:perator
auto_ptr_ref<Window>', giving a stack overflow in very short time; is
this a bug in the code above, in the standard library implementation, or
in the compiler (I know there are subtle issues with temporaries)?
#include <iostream> // std::cout
#include <string> // std::string
#include <memory> // std::auto_ptr
class Window
{
public:
typedef std::auto_ptr<Window> AutoPtr;
virtual ~Window() {}
static AutoPtr newObject() { return AutoPtr( new Window ); }
}; // class Window
class Button: public Window
{
public:
typedef std::auto_ptr<Button> AutoPtr;
static AutoPtr newObject() { return AutoPtr( new Button ); }
}; // class Button
int main()
{
Window::AutoPtr pWindow( Window::newObject() );
Window::AutoPtr pButton( Button::newObject() ); // <--
std::cout << "Onky donk" << std::endl;
}
The line marked with an arrow attempts to convert a Button::AutoPtr to
a Window::AutoPtr.
I had the impression that this should work fine, but compiled with VC
7.1 it just enters an infinite recursion in 'auto_ptr<Button>:perator
auto_ptr_ref<Window>', giving a stack overflow in very short time; is
this a bug in the code above, in the standard library implementation, or
in the compiler (I know there are subtle issues with temporaries)?