按照经验,设计一个类时需要对成员变量进行初始化。C++提供了两种初始化成员变量的途径,让程序员有了一些的选择空间。同时,这也给程序员留下了坑。
初始化类成员变量的途径:
据此,设计以下代码:
#includeusing namespace std::literals;
std::string generate_name_1()
{std::cout<< "Calling generate_name_1() ..."<< std::endl;
return "Alex"s;
}
std::string generate_name_2()
{std::cout<< "Calling generate_name_2() ..."<< std::endl;
return "Brant"s;
}
std::string generate_name_3()
{std::cout<< "Calling generate_name_3() ..."<< std::endl;
return "Chris"s;
}
class person_1
{public:
person_1() = default;
std::string name() const {return m_name; }
private:
std::string m_name{generate_name_1() };
};
class person_2
{public:
person_2() :m_name{generate_name_2() } {}
std::string name() const {return m_name; }
private:
std::string m_name;
};
class person_3
{public:
person_3() :m_name{generate_name_2() } {}
std::string name() const {return m_name; }
private:
std::string m_name{generate_name_1() };
};
class person_4
{public:
person_4() :m_name{generate_name_2() }
{m_name = generate_name_3();
}
std::string name() const {return m_name; }
private:
std::string m_name{generate_name_1() };
};
int main()
{std::cout<< "===== class person_1 ====="<< std::endl;
person_1 person1;
std::cout<< person1.name()<< std::endl;
std::cout<< std::endl;
std::cout<< "===== class person_2 ====="<< std::endl;
person_2 person2;
std::cout<< person2.name()<< std::endl;
std::cout<< std::endl;
std::cout<< "===== class person_3 ====="<< std::endl;
person_3 person3;
std::cout<< person3.name()<< std::endl;
std::cout<< std::endl;
std::cout<< "===== class person_4 ====="<< std::endl;
person_4 person4;
std::cout<< person4.name()<< std::endl;
std::cout<< std::endl;
return EXIT_SUCCESS;
}
上述代码的运行结果如下:
===== class person_1 =====
Calling generate_name_1() ...
Alex
===== class person_2 =====
Calling generate_name_2() ...
Brant
===== class person_3 =====
Calling generate_name_2() ...
Brant
===== class person_4 =====
Calling generate_name_2() ...
Calling generate_name_3() ...
Chris
我们看到:
总的来说就是:赋值会覆盖初始化值,初始化成员列表中的初始化会覆盖定义成员变量时的初始化。
总结你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧