Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
停止C++;类实例化 是否有一种方法来阻止C++类在实例化中出错?比如,可能返回空值?基本上我有一个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; } PDB::PDB(string\u DB\u IP,string\u DB\u USER,string\u DB\u PASS,string\u DB\u DB) :_DB_IP(_DB_IP),_DB_USER(_DB_USER),_DB_PASS(_DB_PASS),_DB_DB(_DB_DB) { mysql\u init(&this->mysql); this->connection=mysql\u real\u connect(&this->mysql,this->\u DB\u IP.c\u str(),this->\u DB\u USER.c\u str(),this->\u DB\u PASS.c\u str(),this->\u DB\u DB.c\u str(),0,0); if(this->connection==NULL)//除了抛出错误之外,我应该在这里做什么? { cout(mysql)result=NULL; }_C++_Class_Object_Constructor_Instantiation - Fatal编程技术网

停止C++;类实例化 是否有一种方法来阻止C++类在实例化中出错?比如,可能返回空值?基本上我有一个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; } PDB::PDB(string\u DB\u IP,string\u DB\u USER,string\u DB\u PASS,string\u DB\u DB) :_DB_IP(_DB_IP),_DB_USER(_DB_USER),_DB_PASS(_DB_PASS),_DB_DB(_DB_DB) { mysql\u init(&this->mysql); this->connection=mysql\u real\u connect(&this->mysql,this->\u DB\u IP.c\u str(),this->\u DB\u USER.c\u str(),this->\u DB\u PASS.c\u str(),this->\u DB\u DB.c\u str(),0,0); if(this->connection==NULL)//除了抛出错误之外,我应该在这里做什么? { cout(mysql)result=NULL; }

停止C++;类实例化 是否有一种方法来阻止C++类在实例化中出错?比如,可能返回空值?基本上我有一个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; } PDB::PDB(string\u DB\u IP,string\u DB\u USER,string\u DB\u PASS,string\u DB\u DB) :_DB_IP(_DB_IP),_DB_USER(_DB_USER),_DB_PASS(_DB_PASS),_DB_DB(_DB_DB) { mysql\u init(&this->mysql); this->connection=mysql\u real\u connect(&this->mysql,this->\u DB\u IP.c\u str(),this->\u DB\u USER.c\u str(),this->\u DB\u PASS.c\u str(),this->\u DB\u DB.c\u str(),0,0); if(this->connection==NULL)//除了抛出错误之外,我应该在这里做什么? { cout(mysql)result=NULL; },c++,class,object,constructor,instantiation,C++,Class,Object,Constructor,Instantiation,在空测试中,我应该怎么做,停止创建等等?抛出异常实际上是在构造过程中指示错误的唯一方法 如果连接正常会失败,请在对象中设置一个标志,并提供一个is_connected()成员函数,并在您的应用程序代码中使用它。如果它正常运行,则抛出异常。前者是C++标准库用于打开文件流的模式。< / P>也许我应该将连接移出构造?或者如果->连接为空,则在查询函数中抛出错误?HMMMZG是G。穿t恤衫的ood标语。嗯,不,不是。例如,std::fstream对象不会以这种方式表示错误。因为std::fstrea

在空测试中,我应该怎么做,停止创建等等?

抛出异常实际上是在构造过程中指示错误的唯一方法


如果连接正常会失败,请在对象中设置一个标志,并提供一个is_connected()成员函数,并在您的应用程序代码中使用它。如果它正常运行,则抛出异常。前者是C++标准库用于打开文件流的模式。< / P>也许我应该将连接移出构造?或者如果->连接为空,则在查询函数中抛出错误?HMMMZG是G。穿t恤衫的ood标语。嗯,不,不是。例如,std::fstream对象不会以这种方式表示错误。因为
std::fstream
不会表示构造函数中的错误。@unquiet mind:是的,这是链接文章中提到的“僵尸”技术的一个示例。有时它可能是“最不坏”的或者,正如Marshall Cline所说,但你必须提出一个很好的论据来说服我,它比抛出要好。@Fred我认为文件流的一个很好的论据是,标准库就是这样做的,而且它工作得很好。我认为处理这样的流比处理抛出异常的流容易得多。@unquiet mind:事实上,IO错误通常不会被注意到(尤其是新手,他们不明白如果输入格式不正确,为什么他们的代码会进入无限循环),因为iostream默认情况下不会抛出任何错误。请注意使用“正常”或“异常情况”等术语(未定义它们)进行解释(这两个术语相互对立)来描述异常使用。可以阅读上述文本来支持使用异常或不使用异常的立场(取决于读者对异常使用和“正常”的现有偏好)“弗莱德确实回答了这个问题——有时你使用异常,有时你不这样做。值得一提的是,异常不是非常可移植的,尤其是在图书馆的边界上。Mozilla C++可移植指南直接禁止使用例外。所以有时是唯一的。另一种选择是,不管是“正常”还是“异常姜黄素”,这个标志都是有效的。“有时你使用异常,有时你不使用异常”有什么帮助?对这个问题的明显回答是“嗯,这就是我问的原因”,因为它只是重复了这个问题。如果是“你想要茶还是咖啡?”的话,语法上有效的答案是“是”,但它仍然没有帮助,因为本质上与我上面指出的原因相同。