Reputation: 5232
I am currently wrapping a library written for C for my C++ program. As to do so I make objects that automatically take care of deleting the C parts when an object goes out of scope. I have done this as an example for an sqlite3_stmt *
:
class auto_stmt_t
{
public:
auto_stmt_t() :
stmt_(NULL)
{
}
virtual ~auto_stmt_t()
{
reset();
}
void reset()
{
if (stmt_ != NULL)
{
::sqlite3_finalize(stmt_);
stmt_ = NULL;
}
}
bool prepare(const std::string &query, sqlite3 *db)
{
reset();
return ::sqlite3_prepare_v2(db, query.c_str(), -1, &stmt_, NULL) == SQLITE_OK;
}
private:
sqlite3_stmt stmt_;
};
Now I want to pass this object to a sqlite
function such as sqlit3_step
which takes a sqlite3_stmt *
as argument.
Is there a way I can just pass my object which then will get auto castet? Something like this:
auto_stmt_t stmt;
stmt.prepare("SELECT bar FROM foo");
::sqlite3_step(stmt);
I have seen that this is possible, which operator will I need to overload to automatically get the sqlite3_stmt *
for this to work?
Upvotes: 1
Views: 112
Reputation: 136425
One way is to add a wrapper member function that calls sqlite3_step
for you and also checks for errors.
Upvotes: 0
Reputation: 117513
Add
operator sqlite3_stmt* () const { return &stmt_; }
to your wrapper class.
Upvotes: 3