Barry <dhb52@126.com> wrote in message...
BobR wrote:
return std::auto_ptr<Pizza>( new Pizza( "Nest" ) );
Thanks for your reply, can you tell me where in the standard talk about
this?
And more, why the example in the OP works with MSVC8 while this one does
NOT?
It does NOT work. Did you read what Peter said?
You can't return an object A pointer where it is expected to return an
std::auto_ptr object. If it does compile and run, it's UB.
What you were trying to do is akin to:
std::string Line("Hello World!");
int Number = Line;
And why all cases works well with STLPort? Is this a bug in STLPort's
auto_ptr?
You'll have to ask that where you got STLPort.
Does this compile for you?
// includes here
struct Pizza{
Pizza( std::string const& name) : name_(name) {}
void Show( std::ostream &out ) const {
out<<name_<<" Pizza"<<std::endl;
}
private:
std::string name_;
};
class AbstractFactory{ public:
virtual std::auto_ptr<Pizza> CreatePizza() = 0;
virtual Pizza* CreatePizza2() = 0;
virtual ~AbstractFactory() {}
};
class NestFactory : public AbstractFactory{
virtual std::auto_ptr<Pizza> CreatePizza(){
return std::auto_ptr<Pizza>( new Pizza( "Nest" ) );
}
virtual Pizza* CreatePizza2(){
return new Pizza( "Nest2" );
}
};
int main(){
std::auto_ptr<AbstractFactory> pFactory( new NestFactory );
std::auto_ptr<Pizza> pPizza = pFactory->CreatePizza();
pPizza->Show( std::cout );
std::auto_ptr<Pizza> pPizza2( pFactory->CreatePizza2() );
pPizza2->Show( std::cout );
return 0;
} // main()
then take this auto_ptr_ref to construct auto_ptr.
that's why the code compiles.
auto_ptr, it seems to be implementation defined.