我正在学习使用Wt(诙谐的)C ++库,并尝试使用singleton即兴创作Dbo :: Session。以下是我创建的MySQL Backend Singleton包装器的片段。
程序编译并以异常运行:MySQL Connect Failure。
在使用 GDB 进行调试时,它显示dbhost
的变量//BREAKPOINT
的值为"localhost"
但是,//STEP1
的值为const std::string dbhost
垃圾。
Wt::Dbo API特别表明MySQL类构造函数只有一个字符串参数,它不是由引用MySQL(const std::string &db, const std::string &dbuser="root",
const std::string &dbpasswd="", const std::string dbhost="localhost",
unsigned int dbport = 0,
const std::string &dbsocket ="/var/run/mysqld/mysqld.sock",
int fractionalSecondsPart = -1);
传递的。头文件中的函数签名是
MySQL(db, dbuser, dbpasswd) // like this
只有三个参数的相同代码传递给超类构造函数
#ifndef CANDBM_MODEL_MYSQL_SINGLETON_H
#define CANDBM_MODEL_MYSQL_SINGLETON_H
#include <Wt/Dbo/Dbo>
#include <Wt/Dbo/backend/MySQL>
namespace dbo = Wt::Dbo;
namespace Candbm { namespace Model {
class MysqlSingleton : public dbo::backend::MySQL
{
private:
static std::string const DB_NAME;
static std::string const DB_USER;
static std::string const DB_PASS;
static std::string const DB_HOST;
static int const DB_PORT;
static MysqlSingleton* mysql;
MysqlSingleton(
const std::string& db,
const std::string& dbuser = "root",
const std::string& dbpass = "",
const std::string dbhost = "localhost",
unsigned int dbport = 0,
const std::string& dbsocket = "/var/run/msyqld/mysqld.sock",
int fractionalSecondsPart = -1
)
: MySQL(db, dbuser, dbpass, dbhost, dbport, dbsocket, // STEP 1
fractionalSecondsPart
) {}
MysqlSingleton(MysqlSingleton const& m) : dbo::backend::MySQL(m) {}
MysqlSingleton operator=(MysqlSingleton const&);
public:
static MysqlSingleton* getBackend() { return instance(); }
static MysqlSingleton* instance();
};
std::string const MysqlSingleton::DB_NAME = "wt_candbm";
std::string const MysqlSingleton::DB_USER = "wtcandbm";
std::string const MysqlSingleton::DB_PASS = "somedummyvalue";
std::string const MysqlSingleton::DB_HOST = "localhost";
int const MysqlSingleton::DB_PORT = 0;
MysqlSingleton* MysqlSingleton::mysql = NULL;
MysqlSingleton* MysqlSingleton::instance()
{
if (!mysql)
{ // BREAKPOINT 1
// EDIT 1: commented the following line
// static std::string dbhost(DB_HOST.begin(), DB_HOST.end());
// EDIT 2: the constant directly passed here ,,,,,,,
mysql = new MysqlSingleton(DB_NAME, DB_USER, DB_PASS, DB_HOST, DB_PORT);
mysql->setProperty("show-queries", "true");
}
return mysql;
}
} }
#endif
将所需的mysql后端对象返回给代码。
以下是用于编译的代码
EDIT 1:2: