c++ - C++;这是确保正确数据成员初始化的正确方法吗?
问题描述
我有一个构造函数:
class Student{
string name;
string last_name;
unsigned score;
...
Student(string, string, int);
// Student.cpp
Student::Student(string name, string last_name, int score) : name(name), last_name(last_name)/
,score( score <= 0 ? 1 : score) {} // notice the 'score' initialization.
我想确保输入例如 Student s("name", "last", -1); 不会弄乱整个事情,因为学生的分数在 [1 , 5] 的区间内,并且分数是“无符号”数据类型。什么类型的分数真的无关紧要,我可以把它作为'int'或'int8_t'(用于节省内存)来解决问题,但我想知道是否有更好的方法来做到这一点?
谢谢大家 :)
解决方案
推荐阅读
- azure-cosmosdb - SqlQuerySpec 参数化查询不返回结果
- php - 如何在 search.php 中使用 wp 循环
- typescript - Visual Studio 2017 CE 中的 TypeScript 调试不起作用
- c# - 将字符串添加到 Hashtable 时,字符串获得无法识别的字符
- java - 解密字符串时出现 InvalidCipherTextException “垫块损坏”
- python - 根据这些行中的值创建包含现有列的行的新列
- java - 如何使用 RestAssured post 方法上传多个图像
- android - Android:AppCompatAcitivity 的构造函数在哪里?
- kubernetes - 在 Kubernetes 集群上应用 Calico 网络时出错
- java - Android 应用程序读写物理内存