C++ 类构造函数c+中的条件+;

C++ 类构造函数c+中的条件+;,c++,class,object,constructor,conditional-statements,C++,Class,Object,Constructor,Conditional Statements,我必须写一个包含个人数据(如姓名、姓氏和年龄)的类定义,条件是年龄不能小于1岁。我试图抓住Constructor类中的一个解释: class Person { public: Person (std::string name, std::string surname, int age) { try{ this -> name = name; this -> surname = surname;

我必须写一个包含个人数据(如姓名、姓氏和年龄)的类定义,条件是年龄不能小于1岁。我试图抓住Constructor类中的一个解释:

class Person {

public:
    Person (std::string name, std::string surname, int age) {
        try{
            this -> name = name;
            this -> surname = surname;
            this -> age = age;

            if(age < 1)
                throw std::string("Error! Age cannot be less than 1!");
       }

       catch(std::string ex){
           cout << ex << endl;
       }
};

private:
    std::string name;
    std::string surname;
    int age;
};
班级人员{
公众:
人员(标准::字符串名称,标准::字符串姓氏,整数年龄){
试一试{
此->名称=名称;
这->姓=姓;
这个->年龄=年龄;
if(年龄<1岁)
抛出std::string(“错误!年龄不能小于1!”);
}
catch(std::string ex){

cout保证您不会创建具有错误值的对象的一种方法是,像您一样在构造函数中抛出异常。只是不要捕获它,让试图创建错误对象的代码捕获它:

class Person
{
public:
    Person(std::string name, std::string surname, int age) {

        if(age < 1)
            throw std::string("Error! Age cannot be less than 1!");

        this->name = name;
        this->surname = surname;
        this->age = age;

    }

private:
    std::string name;
    std::string surname;
    int age;
};
班级人员
{
公众:
人员(标准::字符串名称,标准::字符串姓氏,整数年龄){
if(年龄<1岁)
抛出std::string(“错误!年龄不能小于1!”);
此->名称=名称;
这->姓=姓;
这个->年龄=年龄;
}
私人:
std::字符串名;
std::字符串姓氏;
智力年龄;
};
另外,使用构造函数的初始化列表初始化变量更为常见:

class Person
{
public:
    Person(std::string const& name, std::string const& surname, int age)
    : name(name), surname(surname), age(age) {

        if(age < 1)
            throw std::runtime_error("Error! Age cannot be less than 1!");
    }

private:
    std::string name;
    std::string surname;
    int age;
};
班级人员
{
公众:
人员(标准::字符串常量和姓名,标准::字符串常量和姓氏,整数年龄)
:姓名、姓氏、年龄{
if(年龄<1岁)
throw std::runtime_error(“error!Age不能小于1!”);
}
私人:
std::字符串名;
std::字符串姓氏;
智力年龄;
};
通过引入构造函数,您可以保证在使用对象时该对象是有效的:

int main()
{    
    try
    {
        Person p("John", "Doe", -5);

        // if we get here the person must be valid
    }
    catch(std::exception const& e)
    {
        std::cerr << e.what() << '\n';
        return EXIT_FAILURE;
    }
}
intmain()
{    
尝试
{
人p(“约翰”、“多伊”、-5);
//如果我们到了这里,此人必须是合法的
}
捕获(标准::异常常量和e)
{

std::cerr保证不创建具有错误值的对象的一种方法是像您一样在构造函数中抛出异常。只是不要捕获它,让试图创建错误对象的代码捕获它:

class Person
{
public:
    Person(std::string name, std::string surname, int age) {

        if(age < 1)
            throw std::string("Error! Age cannot be less than 1!");

        this->name = name;
        this->surname = surname;
        this->age = age;

    }

private:
    std::string name;
    std::string surname;
    int age;
};
班级人员
{
公众:
人员(标准::字符串名称,标准::字符串姓氏,整数年龄){
if(年龄<1岁)
抛出std::string(“错误!年龄不能小于1!”);
此->名称=名称;
这->姓=姓;
这个->年龄=年龄;
}
私人:
std::字符串名;
std::字符串姓氏;
智力年龄;
};
另外,使用构造函数的初始化列表初始化变量更为常见:

class Person
{
public:
    Person(std::string const& name, std::string const& surname, int age)
    : name(name), surname(surname), age(age) {

        if(age < 1)
            throw std::runtime_error("Error! Age cannot be less than 1!");
    }

private:
    std::string name;
    std::string surname;
    int age;
};
班级人员
{
公众:
人员(标准::字符串常量和姓名,标准::字符串常量和姓氏,整数年龄)
:姓名、姓氏、年龄{
if(年龄<1岁)
throw std::runtime_error(“error!Age不能小于1!”);
}
私人:
std::字符串名;
std::字符串姓氏;
智力年龄;
};
通过引入构造函数,您可以保证在使用对象时该对象是有效的:

int main()
{    
    try
    {
        Person p("John", "Doe", -5);

        // if we get here the person must be valid
    }
    catch(std::exception const& e)
    {
        std::cerr << e.what() << '\n';
        return EXIT_FAILURE;
    }
}
intmain()
{    
尝试
{
人p(“约翰”、“多伊”、-5);
//如果我们到了这里,此人必须是合法的
}
捕获(标准::异常常量和e)
{

std::cerr不捕获异常。在抛出异常时立即捕获异常没有多大意义。如果捕获到异常,该语言会假定您已修复错误。如果无法修复,则不要捕获并吞下它。捕获(而不是重试)异常会告诉程序您处理了错误。异常不会自行传播。很明显,如果您没有捕获异常,将不会创建对象。@GarrettGutierrez 1“在我看来,在构造函数中引发异常通常是不明智的”如果由于某种原因无法创建对象,您将如何处理这种情况?如果无法使用,为什么要在无效状态下创建对象?2)“由于容器实际上未完全构造,因此不会调用析构函数。”是什么阻止您捕获异常、清理所有已使用的资源并重新抛出异常?不要捕获异常。在抛出异常时立即捕获异常没有多大意义。如果捕获异常,语言会假定您修复了错误。如果无法修复,则不要捕获并吞下它。捕获(而不是重试)异常告诉程序您处理了错误。异常不会自行传播。很明显,如果您没有捕获到异常,对象将不会被创建。@GarretGutierrez 1“在我看来,在构造函数中引发异常通常是不明智的”如果由于某种原因无法创建对象,您将如何处理这种情况?如果无法使用,为什么要在无效状态下创建对象?2)“由于容器实际上未完全构造,因此不会调用析构函数。”是什么阻止了您捕获异常,清理所有已使用的资源,并重新抛出异常?请考虑<代码> STD::RangeEyError < /Cord>,这是从RunTimeOrthError派生的。一个简洁的原因:抛出<代码> STD::异常< /代码>派生对象将是您的异常对象的类型应该描述T的类型。“String”并没有这样做。“DrWordMunn我确实考虑过RangeEx错误,但是,在Balices上,我认为它可能更适合坏的索引值而不是DoGy用户输入(可能是数字或文本)。考虑<代码> STD::RangeEyError < /Cord>,这是从RunTimeOrthError派生的。一个简洁的原因是:抛出<代码> STD::异常< /COD>派生对象将是您的异常对象的类型应该描述异常的类型。“DrWordMunn我确实考虑过RangeEx错误,但是,在巴伦斯,我认为这可能更适合坏的索引值,而不是闪避的用户输入(可能是数字或文本)。