你经常使用const成员吗? [英] Do you use const member a lot?

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

问题描述

很多时候,我发现自己将我的成员变量从

const改回非const。无论最初的目标是多好(b $ b),总有至少一个理由不使用const成员。

(交换,存放在容器中等)

而在Java中,80%的情况,我希望最终对于我的实例

变量。


这让我认为是const成员变量在C ++中有用吗?


也许是因为C ++对象的价值语义,不可变的

编程对C ++来说并不好。


静态const是有用的,const方法很有用,const参数是有用的,const T *很有用,但是找不到const的壁龛

成员变量。


那么,你会用const实例成员变量做什么?你是否使用

呢?

For many times, I''ve found myself changing my member variables from
const back to non-const. No matter how good the original objective
was, there was always at least one reason not to use const members.
(swap, storing it in a container, etc.)
Whereas in Java, 80% of the case, I would want "final" for my instance
variables.

It makes me think that is "const member variable" ever useful in C++?

Maybe because of the value semantics of C++ objects, immutable
programming is just not good for C++.

static const is useful, const methods are useful, const parameters are
useful, const T* is useful, but just cannot find niches for const
member variables.

So, what would you use const instance member variable for? Do you use
it at all?

推荐答案

Ben写道:
Ben wrote:
很多次,我发现自己将成员变量从
const更改为非const。无论原始目标有多好,总有至少一个理由不使用const成员。
(交换,将其存放在容器中等)
而在Java中, 80%的情况,我希望最终对于我的实例
变量。

它让我觉得那是const成员变量。在C ++中是否有用?
For many times, I''ve found myself changing my member variables from
const back to non-const. No matter how good the original objective
was, there was always at least one reason not to use const members.
(swap, storing it in a container, etc.)
Whereas in Java, 80% of the case, I would want "final" for my instance
variables.

It makes me think that is "const member variable" ever useful in C++?




您发现重构和重新设置是好的。


程序设计永远不可变。像'const''或非虚拟的方面可以

确保在升级时,你可以降低破坏异能的几率。

从最弱的结构开始 - 常量,引用,

委托 - 只有重构,指针或继承的重构为

你发现需要。


- -

Phlip
http: //industrialxp.org/community/bi...UserInterfaces




" Ben" <是**** @ combined.com>在消息中写道

news:24 ************************** @ posting.google.c om ...

"Ben" <be****@combined.com> wrote in message
news:24**************************@posting.google.c om...
很多时候,我发现自己将成员变量从
const更改回非const。无论原始目标有多好,总有至少一个理由不使用const成员。
(交换,将其存放在容器中等)
而在Java中, 80%的情况,我希望最终对于我的实例
变量。

它让我觉得那是const成员变量。在C ++中有用吗?

也许是因为C ++对象的价值语义,不可变的编程对C ++来说并不好。

静态const很有用,const方法很有用,const参数很有用,const T *很有用,但是找不到const
成员变量的利基。

那么,你会用什么const实例成员变量为?您是否一直使用它?
For many times, I''ve found myself changing my member variables from
const back to non-const. No matter how good the original objective
was, there was always at least one reason not to use const members.
(swap, storing it in a container, etc.)
Whereas in Java, 80% of the case, I would want "final" for my instance
variables.

It makes me think that is "const member variable" ever useful in C++?

Maybe because of the value semantics of C++ objects, immutable
programming is just not good for C++.

static const is useful, const methods are useful, const parameters are
useful, const T* is useful, but just cannot find niches for const
member variables.

So, what would you use const instance member variable for? Do you use
it at all?




一些非常古老的圣人建议:


遇到新工具时,不要浪费你的时间

四处寻找申请。


决定你需要做什么,然后找到工具

最适合该任务。在寻找

正确的工具时,如果你看到一些不适合你的

任务,只需把它们留在盒子里。

-Mike



Some very old sage advice:

When encountering a new tool, don''t waste your time
going around trying to find an application for it.

Decide what you need to do, then locate the tools
best suited for that task. While looking for the
right tools, if you see some that don''t fit your
task, just leave them in the box.
-Mike


>那么,你将使用const实例成员变量?你根本使用
> So, what would you use const instance member variable for? Do you use
吗?




在我使用的代码中,我没有看到很多const成员变量。

我确实看到const方法和参数被大量使用。


我想很少有成员变量可以初始化的情况

在构造函数中,并且不需要更改

对象的生命周期。


Sandeep

- -
http://www.EventHelix.com/EventStudio

EventStudio 2.0 - 系统架构设计案例工具



In the code I work with, I haven''t seen a lot of const member variables.
I do see const methods and parameters being used a lot.

I guess there are few cases where a member variable can be initialized
in the constructor and does not need to change for the lifetime of the
object.

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - System Architecture Design CASE Tool


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

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