C++ getter 和 setter 最佳风格
Java代码约定简单明了,采用这种风格:(
public:
int GetMyAge(){
return myAge;
}
void SetMyAge(int myAge){
this->myAge = myAge;
}
private:
int myAge;
我知道它“又是同一件事”,但是)我已经阅读了大多数相关问题,但我仍然不知道“用 C++ 实现这一点的最佳方法”和“最官方的”方法。这不可能只是一个偏好问题,可以吗?
编辑:
似乎可以。
in Java code convention is simple and obvious, in this style:
public:
int GetMyAge(){
return myAge;
}
void SetMyAge(int myAge){
this->myAge = myAge;
}
private:
int myAge;
(I know it's "again the same thing", but) I have read most of related questions on SO and I still don't know "the best one" and "the most official" way to do it in C++. It can't be just a matter of preferences, can it?
EDIT:
Seems like it can.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最好根本不要这样做。年龄真的可以这样改变吗?盲目地为所有属性提供 getter 和 setter 是您没有正确设计类的标志。
Best not to do it at all. Can your age actually be changed like that? Blindly providing getters and setters for all properties is a sign you have not designed your class properly.
最好的风格是让您和您的团队能够开发出高质量的软件,并且您的客户会继续为您付费。
这种风格对你和你的团队来说效果如何?您发现它会导致(或防止)错误吗?您觉得维护代码容易吗?您是否对格式争论不休?
回答这些问题,你的问题的答案就会从中产生。
The best style is the one that allows you and your team to make quality software that your clients continue to pay you for.
How does this style work for you and your team? Do you find it causes (or prevents) bugs? Do you find it easy to maintain the code? Do you bicker about the formatting?
Answer those questions and the answer to your question will arise out of them.
一个简单的答案:在 C++ 中,类名通常是大写的(除了 std 类),方法是小写的,一些框架(如 Qt)更喜欢驼峰式命名法,但我更喜欢下划线符号——STL 也是如此。 “auto_ptr”。
类并不总是有单独的
.h
文件,因为这里.java
文件被分割成.h
标头(对于整个包) 和.cpp
实现文件,每个类一个。一些实现/初始化(通常在单独的
TipicalCamelCase.cpp
文件中):下划线样式是相同的,但
我更喜欢它,因为它在标头和实现文件中看起来更干净。
您可以看到函数标题比 java 中的更长。特别是您会看到
templates
(泛型)的 2 行标题是典型的,因此值得将它们分开一些。我认为它应该作为风格介绍。
如果您使用继承,对于 java 风格的工作,请标记除构造函数
virtual
之外的每个函数,以便 @overrides 行为正确。A simple answer: class names are capital in general in c++ (except for the std classes), methods are lower case, some frameworks like Qt prefer camelCase, however I prefer underscore_notation -- and so do the STL see eg. "auto_ptr".
Classes do not always have separate
.h
files, because here a.java
file is split up into a.h
header (for an entire package), and.cpp
implementation files, one per class.Some implementations/initializations (usually in separate
TipicalCamelCase.cpp
file):Underscore style is the same but
I prefer this as it looks cleaner both in the header and in the implementation files.
You can see that function headlines are lengthier than in java. Especially you'll see with
templates
(generics) 2 lines' header is typical, so it is worth to put them a bit more separated.I think it should do as a style intro.
If you use inheritance, for the java-style working, mark every function except the constructors
virtual
so that the @overrides behave correctly.您在上面的代码中编写的语法是正确的。如果您正在寻找经验法则,请以正确设置/获取值的方式对访问器函数进行编码。
EG:
我更愿意将验证“newAge > 10 && newAge < 100”放在不同的函数 IsValidAge 中;即使代码只有一行。从长远来看,小函数有助于维护代码,并帮助新开发人员更好地理解代码。
不过,我想对此发表评论,
将类变量的命名约定与 _myAge 区分开来是一种很好的做法。
编辑
我认为变量名的理解不正确。
myAge 应命名为 _myAge 。
What you have written in the above code is a correct syntax . If you are looking for a thumb rule, code your acccessor functions in such a way that they are set / get exactly the values .
EG :
I would prefer to put the validation "newAge > 10 && newAge < 100" in a different function, IsValidAge ; even if the code is just one line. On the long run, small functions help in maintaining the code, and helps new developers to understand the code better.
However I would like to comment on this
It is good practice to differentiate the nameing convention of the class varaiable to _myAge .
EDIT
I think the variable name was comprehended improperly .
myAge should be named _myAge .