停止C ++类实例化 [英] Stop C++ Class Instantiation

查看:127
本文介绍了停止C ++类实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果实例化中有错误,是否有一种方法来停止C ++类?喜欢,返回NULL也许?基本上我有一个MySQL的包装类,并且构造函数做连接,但如果连接失败,我想要的对象,um,无用?

Is there a way to stop a C++ class if there is an error in the instantiation? Like, return NULL maybe? Basically I have a wrapper class for MySQL, and the constructor does the connecting, but if the connection fails, I want the object to be, um, useless?

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测试中做什么,停止创建等? / p>

What should I do in the NULL test, to stop creation, etc?

推荐答案

抛出异常是在构建过程中指出错误的唯一方法。

Throwing an exception is really the only way to indicate an error during construction.

http://www.parashift.com/c++ -faq-lite / exceptions.html#faq-17.8

这篇关于停止C ++类实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆