我只是想知道为什么我能够传递所有权而不是以下代码中的引用?我知道使用std::unique_ptr< car > pcar( new toyota() );
会让我通过参考,但为什么这不适用于所有权转让?
#include <memory>
class car{};
class toyota : public car{};
void someProcess( std::unique_ptr< car > vehicle )
{
//Ownership passed.
}
void otherProcess( std::unique_ptr< car > const & vehicle )
{
//Reference passed.
}
int main( int argc, char ** argv )
{
std::unique_ptr< toyota > pcar( new toyota() );
someProcess( std::move( pcar ) ); //Allowed to pass through car interface.
pcar.reset( new toyota() );
otherProcess( pcar ); //Compiler error, can't pass reference even when car interface is implemented.
return 0;
}