Make your class noncopyable. In C++11:
class Database {
public:
Database(const Database&) = delete;
Database& operator=(const Database&) = delete;
};
In C++03:
class Database {
private: // inaccessible
Database(const Database&);
Database& operator=(const Database&);
};
Or with Boost:
#include <boost/noncopyable.hpp>
class Database : private boost::noncopyable {
};
In C++11 I would also make the object Movable
by giving it a MoveConstructor and Move assignment operators. There you would assign the handle (or whatever your db gives you) to the new object and use some flag in the old object to indicate that nothing needs to be closed.
Don't forget to implement swap
as well!
class Database {
// This might look odd, but is the standard way to do it without a namespace.
// If you have a namespace surrounding Database, put it there.
friend void swap(Database& a, Database& b) { /* code to swap a and b */ }
};
Also: setting some value to false in your destructor has no effect. Nothing should ever be able to see the change.
Or using a unique_ptr
/shared_ptr
with a custom deleter:
struct CloseDatabase {
void operator()(sqlite* x) { sqlite3_close(x); }
};
typedef std::unique_ptr<sqlite3, CloseDatabase> Database;