本文介绍了“set / get”的实现方式是什么?会员功能更好?请帮忙。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想向具有C ++经验的人学习,以及以下几种方式构建获取/设置的方式。会员功能在可用性,速度等方面最好是 。 以下课程用作示例: class MyClass { public: //会员功能会在这里。 私人: int data_; }; 方式#1: int getData()const; void setData(const int); 方式#2: int getData()const; int setData( INT); //返回data_ 方式#3: int data()const; void data(int); 方式#4: int data()const; int data(int); //返回数据_ 我的偏好是使用第四种方式,但我想知道哪些 列出的方式应该是首选的,或者如果还有其他任何方式可以包含 。 另外,我注意到进入OOA和OOD的人似乎使用了getBlah,并且 setBlah。为什么会这样?这些人中的大多数都是由一个来源充实的,这就是为什么他们都有同样的方式这样做? 谢谢。Hi, I would like learn from people with experience in C++, which of thefollowing styles of way to construct "get/set" member functions would be thebest in terms of usability, speed, et cetera.The following class with be used as an example:class MyClass {public:// member function would go here.private:int data_;};Way #1:int getData() const;void setData(const int);Way #2:int getData() const;int setData(int); // returns data_Way #3:int data() const;void data(int);Way #4:int data() const;int data(int); // returns data_My preference would be to use the fourth way, but I would like to know whichof the ways listed should be prefered or if there are any other ways thatshould have been included.Also, I notice that people who are into OOA and OOD seem to use getBlah, andsetBlah. Why is this the case? Were most of these individuals influened byone source and that is why they all share the same way of doing it?Thanks.推荐答案 第五种方式是 int& data(); int data()const; 但是我不喜欢它,返回一个或多或少的引用会让你使用 a内部成员变量。我个人会用你的第一种方法。得到和 设置不同,他们应该得到不同的名字。我没有看到 需要从set中返回先前的值,如果我需要的话我会在设置之前调用get 。再一次,这似乎是我的清晰度,应该是你的主要考虑因素,而不是速度。 johnA fifth way isint& data();int data() const;but I don''t like it, returning a reference more or less commits you to usinga member variable internally. Personally I''d use your first method. Get andset are different enough that they deserve different names. I don''t see theneed to return the previous value from set, if I needed that I''d call getbefore set. Again this seems a matter of clarity to me, which should be yourmain consideration, not speed.john the which 和 and 第五种方式是 int& data(); int data()const; 但我不喜欢它,返回一个或多或少的引用会让你在内部使用一个成员变量。我个人会用你的第一种方法。 Get和设置不同,他们应该得到不同的名字。我没有看到需要从set中返回之前的值,如果我需要的话我会在设置之前调用get 。对我来说这似乎是一个清晰的问题,这应该是你的主要考虑因素,而不是速度。 A fifth way is int& data(); int data() const; but I don''t like it, returning a reference more or less commits you to using a member variable internally. Personally I''d use your first method. Get and set are different enough that they deserve different names. I don''t see the need to return the previous value from set, if I needed that I''d call get before set. Again this seems a matter of clarity to me, which should be your main consideration, not speed. 并且有第六种方式是变体第5页。 AccessorReference< data_attributes> data(); const AccessorReference< data_attributes> data()const; 其中AccessorReference类型的对象< data_attributes>有 分配和投射操作符,可以达到您的预期。 它解决了您描述的问题承诺使用会员 变量。这允许你有一个接口来获取和设置一个 变量,无论它是一个成员变量还是一些不同的东西。 我已经当你可能还有一个变种时,你可以使用这种技术获得或设置数据。 例如 obj.data()(ScopingName)= 1; obj.data()(NotThrow)= 1;And there is a 6th way which is a variant of the 5th.AccessorReference<data_attributes> data();const AccessorReference<data_attributes> data() const;Where an object of type AccessorReference<data_attributes> hasassignment and cast operators that do what you expect.It solves the problem you describe "commits you to using a membervariable". This allows you to have a single interface to get and set avariable regardless if it''s a member variable or somthing different.I''ve used this technique when you might also have a variant on HOW youget or set the data.e.g.obj.data()(ScopingName) = 1;obj.data()(NotThrow) = 1; 也许是因为在OOA和OOD圈子里在语言中立的情况下,他们不能假设参数被考虑用于多态性吗?只需一个 猜。 JayMaybe because in OOA and OOD circles, being language neutral, they can''tassume that parameters are taken into account for polymorphism? Just aguess.Jay 这篇关于“set / get”的实现方式是什么?会员功能更好?请帮忙。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-01 19:18