吸气剂和二传手 [英] getters and setters

查看:68
本文介绍了吸气剂和二传手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉Java中的get和set函数范例,但是在C ++中推荐的设计是什么?应该是这样的:


foo& getFoo();

void setFoo(foo& f);


或类似:


foo& foo();

const foo& foo()const;


或上面的一些组合?


-

我只是一个海市蜃楼。

I''m familiar with get and set function paradigms from Java, but what''s
the recommended design for such in C++? Should it be like so:

foo& getFoo();
void setFoo(foo& f);

or like so:

foo& foo();
const foo& foo() const;

or some combination of the above?

--
I am only a mirage.

推荐答案

kelvSYC写道:
kelvSYC wrote:
我熟悉get和set功能范例来自Java,但在C ++中推荐的设计是什么?应该是这样的:

foo& getFoo();
void setFoo(foo& f);

或者像这样:

foo& foo();
const foo& foo()const;

或上面的一些组合?
I''m familiar with get and set function paradigms from Java, but what''s
the recommended design for such in C++? Should it be like so:

foo& getFoo();
void setFoo(foo& f);

or like so:

foo& foo();
const foo& foo() const;

or some combination of the above?




我的偏好是:


class C {

int x(){return _x; }

void x(int x_){_ x = x_; }

私人:

int _x;

};


但我相信我不是在这个问题上占绝大多数。

-

如果我们的假设是关于任何事情而不是关于某一个或多个特定事物,那么我们的扣除构成数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题.- Bertrand Russell


kelvSYC写道:
kelvSYC wrote:
我熟悉Java中的get和set函数范例,但推荐的设计是什么?在C ++中?应该是这样的:

foo&的getFoo();


添加


foo getFoo()const;


或更改为


foo const& getFoo()const;


除非你想要改变它,否则不需要将''foo''作为非const的引用,为此,有''setFoo()''。

void setFoo(foo& f);

或者像这样:

foo& ; foo();
const foo& foo()const;

或上面的一些组合?
I''m familiar with get and set function paradigms from Java, but what''s
the recommended design for such in C++? Should it be like so:

foo& getFoo();
Add

foo getFoo() const;

or change to

foo const& getFoo() const;

there is no need to get ''foo'' as a ref to non-const unless you want to
change it, and for that there is ''setFoo()''.
void setFoo(foo& f);

or like so:

foo& foo();
const foo& foo() const;

or some combination of the above?




我更喜欢前者(我的更正)。我甚至_recommend_前者。

后者相当令人困惑。当然,除非您考虑转换运算符



运算符foo&();

运算符foo()const;


所以为了回答你的问题,我不知道推荐的设计是什么。是因为每个人都有他们的偏好。


V



I prefer the former (with my corrections). I even _recommend_ the former.
The latter is rather confusing. Unless, of course, you consider
conversion operators:

operator foo&();
operator foo() const;

So to answer your question, I don''t know what "the recommended design" is
since everybody has their preferences.

V


Steven T. Hatton写道:< br>
Steven T. Hatton wrote:
kelvSYC写道:

kelvSYC wrote:

我熟悉Java中的get和set函数范例,但是它是什么? br /> C ++中推荐的设计?应该是这样的:

foo& getFoo();
void setFoo(foo& f);

或者像这样:

foo& foo();
const foo& foo()const;

或以上的某些组合?

我的偏好是:

C类{
int x( ){return _x; }
I''m familiar with get and set function paradigms from Java, but what''s
the recommended design for such in C++? Should it be like so:

foo& getFoo();
void setFoo(foo& f);

or like so:

foo& foo();
const foo& foo() const;

or some combination of the above?
My preference is:

class C{
int x() { return _x; }



int x()const {return _x; }

void x(int x_){_ x = x_; }
私人:
int _x;
};

但我相信我并不占多数。



int x() const { return _x; }
void x(int x_) { _x = x_; }
private:
int _x;
};

But I believe I am not in the majority on this.




我明白为什么。如果函数接受参数,那么

函数的名称应该是动词。

C c;

cx(5);


那是做什么的?现在,如果它是


cx()= 5;





c。 set_x(5);


我不会争辩。


V



I can see why. If the function takes an argument, then the name of the
function should be a verb.
C c;
c.x(5);

what does that do? Now, if it were

c.x() = 5;

or

c.set_x(5);

I''d not argue.

V


这篇关于吸气剂和二传手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆