Why doesn't auto_ptr construction work using = syntax

前端 未结 4 902
失恋的感觉
失恋的感觉 2020-12-16 20:04

I ran into a compiler error that didn\'t make much sense to me:

#include 
using namespace std;

auto_ptr table = db->query(\"se         
4条回答
  •  一向
    一向 (楼主)
    2020-12-16 20:35

    You need to use

    auto_ptr
table = auto_ptr
(db->query("select * from t"));

auto_ptr does not define an assignment operator for it's template type. The only allowed assignment is from another auto_ptr (and it's constructor from the pointer is explicit). This is done to protect accidental misuse of auto_ptr, as auto_ptr assumes ownership of the memory.

My guess is that you need the assignment form to use multiple queries after another like:

// initialize using constructor
auto_ptr
table(db->query("select * from t1")); ... // new query using assignment table = auto_ptr
(db->query("select * from t2")); ... // another query using assignment table = auto_ptr
(db->query("select * from t3"));

提交回复
热议问题