C++:防止类初始化

ace*_*ace 1 c++

我有一个类,在类构造函数中我想检查已传递的几个参数,如果任何参数未通过检查,我想阻止该类初始化.我怎样才能做到这一点 ?

Class MyClass
{
 MyClass(int no);
};
MyClass::MyClass(int no)
{
  if(no<0) // Prevent the Class from Initialisation
}

void main()
{
       MyClass myobj(-1);

       // How to check if myobj is an objecT???
       //  if(myobj!=null) ???
 }
Run Code Online (Sandbox Code Playgroud)

Kri*_*son 15

抛出一个例外.


sbi*_*sbi 6

在你的具体情况下:拿一个unsigned int.

一般来说:抛出异常.