使用const引用的getter [英] The use of const reference instear of getter

查看:70
本文介绍了使用const引用的getter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人;


据我了解getter方法背后的想法,它用于确定
确保一个班级的私人成员是适当地返回

调用对象。


但是,如果我在私有成员时感兴趣的是

不允许修改该成员的价值(只读

成员),然后如何执行以下操作:


A级{

public:

const int& ref_to_priv_member;

A():ref_to_priv_member(priv_member){

/ * Blah * /

}


private:

int private_member;

};


我想知道C ++专家对此的看法如果有$

是这个的任何副作用。

另外从性能的角度来看,还没有使用这个mor

是否比使用吸气剂更有效?


期待听到您的意见。

解决方案

都灵< om *********@gmail.com写道:


亲爱的所有人;


据我所知getter方法背后的想法,它用于

确保一个类的私人memers被适当地返回到

调用对象。


但是,如果我对私有会员感兴趣的是

不允许修改该成员的价值(rea) d-only

成员),然后如何做以下事项:

[...]

我想知道的意见关于这个的C ++专家,如果有
是这个的任何副作用。

从性能的角度来看,也没有使用这个mor

比使用吸气剂更有效?


期待听到您的意见。


A级{

public:

const int& ref_to_priv_member;



相反,请执行以下操作:


inline int ref_to_priv_member(void)const {return private_member;这样你只有一个正确的值,如果实现应该改变你的b $ b,你就不必改变客户端代码了(b / b)只重新编译)。另一方面,

,内联成员函数将被优化,

您将获得与数据成员相同的代码。

-

__Pascal Bourguignon__


2008-08-04 13:31,都灵写道:
< blockquote class =post_quotes>
亲爱的;


据我了解getter方法背后的想法,它用于

make确保一个班级的私人课程被适当地归还给调用对象。


然而,如果所有我对私人成员感兴趣的话都是

禁止修改该成员的价值(只读

成员),然后如何执行以下操作:


级A {

public:

const int& ref_to_priv_member;

A():ref_to_priv_member(priv_member){

/ * Blah * /

}


private:

int private_member;

};


我想知道C ++专家对此的看法如果有$

是这个的任何副作用。

另外从性能的角度来看,还没有使用这个mor

比使用吸气剂更有效吗?



任何好的编译器都可能将getter函数减少到相同的

的东西,除了你可以,如果你想,你可以轻松对getter函数进行更改

(例如更改返回值肯定是

的情况)。


-

Erik Wikstr ?? m


2008-08-04 13:42,Pascal J. Bourguignon写道:


都灵< om ********* @ gmail.comwrites:


>亲爱的所有人;

据我所知,getter方法背后的想法,它用于确保一个类的私人memers被适当地返回到调用对象。
不允许修改该成员的价值(只读
成员),那么如何执行以下操作:
[...]
我想知道的意见关于这个问题的C ++专家以及是否有这样的副作用。
从性能的角度来看,使用这个技术还不比使用吸气剂更有效吗? />
期待听到您的意见。


> A类{
public:
const int& ref_to_priv_member;



相反,请执行以下操作:


inline int ref_to_priv_member(void)const {return private_member; }



Nitpick:

inline是多余的,因为该函数是在类

定义中定义的,并且在C ++我们通常写foo()而不是foo(void),

后者被认为是坏风格。


-

Erik Wikstr ?? m


Dear all;

As far as I understand the idea behind getter methods, it is used to
make sure that private memers of a class is returned appropriately to
the calling object.

However, if all I am interested in when making a member private is to
disallow the modification of the value of that member (read-only
member), then how about doing the following:

class A {
public :
const int& ref_to_priv_member;
A() : ref_to_priv_member(priv_member) {
/* Blah */
}

private :
int private_member;
};

I would like to know the opinion of C++ experts on this and if there
are any side-effects of this.
Also from the perferformance point of view, isn''t using this mor
effecient than using a getter?

Looking forwards to hearing your opinions.

解决方案

Turin <om*********@gmail.comwrites:

Dear all;

As far as I understand the idea behind getter methods, it is used to
make sure that private memers of a class is returned appropriately to
the calling object.

However, if all I am interested in when making a member private is to
disallow the modification of the value of that member (read-only
member), then how about doing the following:
[...]
I would like to know the opinion of C++ experts on this and if there
are any side-effects of this.
Also from the perferformance point of view, isn''t using this mor
effecient than using a getter?

Looking forwards to hearing your opinions.

class A {
public :
const int& ref_to_priv_member;

Instead, do this:

inline int ref_to_priv_member(void) const { return private_member; }

this way you have only a right-value, and if the implementation should
change, you won''t have to change the client code (only recompile). On
the other hand, inline member functions will be optimized out and
you''ll get the same code as with a data member.

--
__Pascal Bourguignon__


On 2008-08-04 13:31, Turin wrote:

Dear all;

As far as I understand the idea behind getter methods, it is used to
make sure that private memers of a class is returned appropriately to
the calling object.

However, if all I am interested in when making a member private is to
disallow the modification of the value of that member (read-only
member), then how about doing the following:

class A {
public :
const int& ref_to_priv_member;
A() : ref_to_priv_member(priv_member) {
/* Blah */
}

private :
int private_member;
};

I would like to know the opinion of C++ experts on this and if there
are any side-effects of this.
Also from the perferformance point of view, isn''t using this mor
effecient than using a getter?

Any good compiler will probably reduce a getter function to the same
thing, with the exception that you can, if you want, easily make changes
to the getter function (such as changing the returned value is certain
cases).

--
Erik Wikstr??m


On 2008-08-04 13:42, Pascal J. Bourguignon wrote:

Turin <om*********@gmail.comwrites:

>Dear all;

As far as I understand the idea behind getter methods, it is used to
make sure that private memers of a class is returned appropriately to
the calling object.

However, if all I am interested in when making a member private is to
disallow the modification of the value of that member (read-only
member), then how about doing the following:
[...]
I would like to know the opinion of C++ experts on this and if there
are any side-effects of this.
Also from the perferformance point of view, isn''t using this mor
effecient than using a getter?

Looking forwards to hearing your opinions.

>class A {
public :
const int& ref_to_priv_member;


Instead, do this:

inline int ref_to_priv_member(void) const { return private_member; }

Nitpick:
inline is superfluous since the function is defined in the class
definition, and in C++ we usually write foo() and not foo(void), the
latter is considered bad style.

--
Erik Wikstr??m


这篇关于使用const引用的getter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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