c++ - Using smart pointers with MySQL Connector -


most tutorials related mysql connector libraries assume, user use raw pointers. i'd use smart pointers instead. i've written following class:

class database{     private:         bool _connected = false;         std::shared_ptr<sql::driver> _driver;         std::shared_ptr<sql::connection> _connection;         std::shared_ptr<sql::statement> _statement;         std::shared_ptr<sql::resultset> _resource;     public:         database();         ~database();         bool connect(const std::string &ip, const std::string &user, const std::string password);         bool connected(); }; 

i'm trying implement connect function, receive following error during compilation:

/usr/include/c++/5.3.0/ext/new_allocator.h:120:4: error: invalid new-expression of abstract class type ‘sql::driver’ { ::new((void *)__p) _up(std::forward<_args>(__args)...); } 

it caused following line of code:

this->_driver = std::make_shared<sql::driver>(get_driver_instance()); 

what doing wrong? i've found few examples smart pointers, in every single 1 of them sql::driver instance raw pointer. impossible assign result of get_driver_instance() function smart pointer?

update:

i think should use reset function instead of make_shared template. unfortunately this:

this->_driver.reset(get_driver_instance()); 

didn't solve problem, got error:

/usr/include/cppconn/driver.h:39:10: error: ‘virtual sql::driver::~driver()’ protected virtual ~driver() {} 

i guess shared_ptr unable "claim" driver's destructor, because it's protected (as said in error). there workaround? or maybe should use raw pointer when dealing sql::driver?

the resulting driver object pointer get_driver_instance() pointer static storage object afaik , pointer may not deleted. not need smart pointer managing life time. static objects destroyed when program ends. other objects in post (sql::connection, sql::statement, sql::resultset) need deleted can use smart pointer managing those.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -