“set / get”的实现方式是什么?会员功能更好?请帮忙。 [英] Which implementation of "set/get" member functions is better? Please help.

查看:62
本文介绍了“set / get”的实现方式是什么?会员功能更好?请帮忙。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向具有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 the
following styles of way to construct "get/set" member functions would be the
best 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 which
of the ways listed should be prefered or if there are any other ways that
should have been included.

Also, I notice that people who are into OOA and OOD seem to use getBlah, and
setBlah. Why is this the case? Were most of these individuals influened by
one source and that is why they all share the same way of doing it?

Thanks.

推荐答案



" Me" <人*********** @ rogers.com>在消息中写道

news:rf ********************* @ news04.bloor.is.net.c able.rogers.com ...

"Me" <al***********@rogers.com> wrote in message
news:rf*********************@news04.bloor.is.net.c able.rogers.com...
我想向具有C ++经验的人学习,其中以下哪种方式构造获取/设置?成员函数在可用性,速度等方面最好是


下面的类用作例子:
class MyClass {
public :
//成员函数会在这里。
私有:
int data_;
};

方式#1:
int getData ()const;
void setData(const int);

方式#2:
int getData()const;
int setData(int); //返回数据_

方式#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 the
following styles of way to construct "get/set" member functions would be the best 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 which of the ways listed should be prefered or if there are any other ways that
should have been included.

Also, I notice that people who are into OOA and OOD seem to use getBlah, and setBlah. Why is this the case? Were most of these individuals influened by
one source and that is why they all share the same way of doing it?

Thanks.




第五种方式是


int& data();

int data()const;


但是我不喜欢它,返回一个或多或少的引用会让你使用

a内部成员变量。我个人会用你的第一种方法。得到和

设置不同,他们应该得到不同的名字。我没有看到

需要从set中返回先前的值,如果我需要的话我会在设置之前调用get

。再一次,这似乎是我的清晰度,应该是你的主要考虑因素,而不是速度。


john



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.

john

< br>

John Harrison写道:
John Harrison wrote:
" Me" <人*********** @ rogers.com>在消息中写道
新闻:rf ********************* @ news04.bloor.is.net.c able.rogers.com ...
"Me" <al***********@rogers.com> wrote in message
news:rf*********************@news04.bloor.is.net.c able.rogers.com...
您好,我想向具有C ++经验的人学习,以下哪种方式构建获取/设置?成员函数将
Hi, I would like learn from people with experience in C++, which of the
following styles of way to construct "get/set" member functions would be





the

在可用性,速度等方面最佳。

以下以类为例:
类MyClass {
公共:
//成员函数将在这里。
私有:
int data_;
方式#1:
int getData()const;
void setData(const int);

方式#2:
int getData()const;
int setData(int); //返回数据_

方式#3:
int data()const;
void data(int);

方式#4:
int data()const;
int data(int); //返回data_

我的偏好是使用第四种方式,但我想知道
best 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






which

列出的方式应该是首选的,或者是否还有其他方式应该包括在内。

另外,我注意到那些进入OOA和OOD的人似乎都在使用getBlah,
of the ways listed should be prefered or if there are any other ways that
should have been included.

Also, I notice that people who are into OOA and OOD seem to use getBlah,






and

setBlah。为什么会这样?这些人中的大多数是否都被一个来源所影响,这就是为什么他们都有同样的方式来做这件事?

谢谢。
setBlah. Why is this the case? Were most of these individuals influened by
one source and that is why they all share the same way of doing it?

Thanks.



第五种方式是

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> has
assignment and cast operators that do what you expect.

It solves the problem you describe "commits you to using a member
variable". This allows you to have a single interface to get and set a
variable regardless if it''s a member variable or somthing different.

I''ve used this technique when you might also have a variant on HOW you
get or set the data.

e.g.

obj.data()(ScopingName) = 1;

obj.data()(NotThrow) = 1;




我 <人*********** @ rogers.com>在消息中写道

news:rf ********************* @ news04.bloor.is.net.c able.rogers.com ...

"Me" <al***********@rogers.com> wrote in message
news:rf*********************@news04.bloor.is.net.c able.rogers.com...
我想向具有C ++经验的人学习,其中以下哪种方式构造获取/设置?成员函数在可用性,速度等方面最好是


下面的类用作例子:
class MyClass {
public :
//成员函数会在这里。
私有:
int data_;
};

方式#1:
int getData ()const;
void setData(const int);

方式#2:
int getData()const;
int setData(int); //返回数据_

方式#3:
int data()const;
void data(int);


我的偏好是#3。这就是我在我的个人代码中这样做的方式,虽然在工作中我们使用#b $ b。#

另外,我注意到进入OOA和OOD的人似乎都在使用getBlah,
和setBlah。为什么会这样?这些人中的大多数是否被一个来源所影响,这就是为什么他们都有相同的做法?
Hi, I would like learn from people with experience in C++, which of the
following styles of way to construct "get/set" member functions would be the best 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);

My preference is for #3. This the way I do it in my personal code, though
at work we use #1.
Also, I notice that people who are into OOA and OOD seem to use getBlah, and setBlah. Why is this the case? Were most of these individuals influened by
one source and that is why they all share the same way of doing it?




也许是因为在OOA和OOD圈子里在语言中立的情况下,他们不能假设参数被考虑用于多态性吗?只需一个

猜。


Jay



Maybe because in OOA and OOD circles, being language neutral, they can''t
assume that parameters are taken into account for polymorphism? Just a
guess.

Jay


这篇关于“set / get”的实现方式是什么?会员功能更好?请帮忙。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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