【发布时间】:2011-05-27 09:45:09
【问题描述】:
如果实例化出现错误,有没有办法停止 C++ 类?就像,返回 NULL 也许?基本上我有一个MySQL的包装类,构造函数进行连接,但是如果连接失败,我希望这个对象,嗯,没用?
PDB::PDB(string _DB_IP, string _DB_USER, string _DB_PASS, string _DB_DB)
: _DB_IP( _DB_IP ), _DB_USER( _DB_USER ), _DB_PASS( _DB_PASS ), _DB_DB( _DB_DB )
{
mysql_init(&this->mysql);
this->connection = mysql_real_connect(&this->mysql, this->_DB_IP.c_str(), this->_DB_USER.c_str(), this->_DB_PASS.c_str(), this->_DB_DB.c_str(), 0, 0, 0);
if( this->connection == NULL ) // WHAT SHOULD I DO HERE, OTHER THAN THROW AN ERROR?
{
cout << mysql_error(&this->mysql) << endl;
}
this->result = NULL;
}
在 NULL 测试中我应该做什么,停止创建等?
【问题讨论】:
-
也许我应该将连接移出构造?或者如果 this->connection 为空,则在查询函数中抛出错误?嗯
-
这是一个很好的标语穿在T恤上。
标签: c++ class object constructor instantiation