默认构造函数全部封装在头文件中
今天在我的课堂上,我们进行了继承练习。我们要编写一个 UnderGrad 类,该类继承自 Student 类,而 Student 类又继承自 Person 类。
Person 类有 3 个变量:姓名、地址和生日。
学生有 ID 号、专业和学位类型。
最后,本科生有一个以前的高中和学分获得变量。
当我为每个类编写头文件时,默认构造函数看起来像这样:
//snippet from UnderGrad.h
Undergrad(string nm = "", string add = "", string bday = "", int id = 0, string maj = "", string degtyp = "", string hs = "", int cred = 0) : Student(nm,add,bday,id,maj,degtyp) {
highSchool = hs;
credits = cred;
};
//snippet from Student.h
Student(string nm = "", string add = "", string bday = "", int id = 0, string maj = "", string degtyp = "") : Person(nm,add,bday){
stuId = id;
major = maj;
degreeType = degtyp;
};
//snippet from Person.h
Person(string nm = "", string add = "", string bday = ""){
name = nm;
address = add;
bDay = bday;
};
我的教授说这是不可读的,而且它并不是真正这样做的。
我想知道,以这种方式创建默认构造函数有什么问题?有什么问题吗?有更好的方法吗?
While in my class today, we had an exercise in inheritance. We were to write a UnderGrad class that inherited from a Student class which inherited from a Person class.
The Person had 3 variables to the class a name, an address and a birthday.
The Student had an ID number, Major and degree type.
And finally the Undergrad had a previous high school and credits earned variables.
When I wrote the header file for each of the classes, the default constructor looked something like this:
//snippet from UnderGrad.h
Undergrad(string nm = "", string add = "", string bday = "", int id = 0, string maj = "", string degtyp = "", string hs = "", int cred = 0) : Student(nm,add,bday,id,maj,degtyp) {
highSchool = hs;
credits = cred;
};
//snippet from Student.h
Student(string nm = "", string add = "", string bday = "", int id = 0, string maj = "", string degtyp = "") : Person(nm,add,bday){
stuId = id;
major = maj;
degreeType = degtyp;
};
//snippet from Person.h
Person(string nm = "", string add = "", string bday = ""){
name = nm;
address = add;
bDay = bday;
};
My professor said that this is unreadable and that its not really done this way.
I was wondering, what is wrong with creating default constructors this way? Is there anything wrong? Is there a better way of doing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有代码上下文的好处,我只需要更改这些构造函数的一些内容。
我会考虑将构造函数的定义移动到单独的源文件中。这将允许更改定义,而不会触发其余代码的重新编译。对于小型程序,或者非常简单的构造函数,如果标头不发生变化,就不可能发生变化,这可能不值得付出努力。
我将更改构造函数的定义以使用成员初始值设定项列表,而不是在构造函数主体中分配成员。 (请参阅 Als 的答案以获取解释)。
我会删除函数定义末尾的分号,它们完全不需要。
您的头文件中似乎有一个
using namespace ::std
或using ::std::string
。这是不好的做法,因为这意味着包含标头的代码将应用此using
。这可能会使客户端代码的含义发生变化,或者使客户端代码变得不明确而无法编译。我将删除using
,而是使用std::string
的限定名称。这些改变付诸实施:
Without the benefit of the context of the code, there are only a few things that I would change about these constructors.
I would consider moving the definitions of the constructors into separate source files. This will allow the definitions to be changed without triggering a recompile of the rest of your code. For small programs, or very simple constructors that are unlikely to change without the header also changing, this is probably not worth the effort.
I would change the definitions of the constructors to use the member initialiser list instead of assigning members in the constructor body. (See the answer from Als for an explanation).
I would remove the semicolons at the ends of the function definitions, they are totally unneeded.
I would split the long constructors into several lines, to avoid overly long lines of code (long lines may not fit in your editor, and could be hard to perform diffs and merges on).
It appears that you have a
using namespace ::std
or ausing ::std::string
somewhere in your header file. This is bad practice, because it means that code which includes your headers will have thisusing
applied. This could make the meaning of the client code change, or make the client code become ambiguous and fail to compile. I would remove theusing
, and instead use a qualified name forstd::string
.These changes put into action:
语义上很好,尽管您可以对成员字符串使用初始化程序而不是分配每个字符串。
从风格上来说,你的台词有点长,但这就是我能认真对待的问题。
Semantically fine, though you could possibly use initialisers for the member strings instead of assigning each one.
Stylistically, your lines are a little long, but that's about all I could take serious issue with.