Question about store data
-
Hello, I have a question, I am implementing a single tone class for connecting to databases, the main task is to place the database in a pool when connecting, I use the PIMPL approach to store data, but here is the question, if a smart pointer is used, then it cannot be copied and placed in a pool, and in principle the logic is broken, it seems to me, what is the best way to implement this?
DbConnection.h
Class DbConnection{
private:
DbConnection() = default;
struct Data;
std::unique_ptr<Data> pData;
//other code
public:
QList<std::unique_ptr<Data>> poolConnections;
bool connect();
//
}DbConnection.cpp
struct DbConnection::Data{
QString host;
QString port;
//other
}bool connect(){
….
how to add each new connection to pool?
} -
Hello, I have a question, I am implementing a single tone class for connecting to databases, the main task is to place the database in a pool when connecting, I use the PIMPL approach to store data, but here is the question, if a smart pointer is used, then it cannot be copied and placed in a pool, and in principle the logic is broken, it seems to me, what is the best way to implement this?
DbConnection.h
Class DbConnection{
private:
DbConnection() = default;
struct Data;
std::unique_ptr<Data> pData;
//other code
public:
QList<std::unique_ptr<Data>> poolConnections;
bool connect();
//
}DbConnection.cpp
struct DbConnection::Data{
QString host;
QString port;
//other
}bool connect(){
….
how to add each new connection to pool?
} -
Hi, your design seems a bit unusual but the easiest way is to store one instance/smart pointer of something like
ConnectionData
in your singleton (instead of a list). And inConnectionData
you store a list of your actual connection data.