包装int [英] wrapping int

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

问题描述

你好!


我有点担心int类型没有被初始化为零

默认情况下,

在大型项目中非常不方便。


您将int包装到轻量级和

完全透明的类中的积极体验是什么,它优雅地解决了这个问题?


-

弗拉基米尔

Hello!

I''m a little bit worried about type int not being initialized to zero
by default,
which is very unconvenient in large projects.

What is your positive experience in wrapping int to a lightweight and
completely transparent class which elegantly solves this problem?

--
Vladimir

推荐答案

>我有点担心类型int没有被初始化为零
> I''m a little bit worried about type int not being initialized to zero
默认情况下,
这在大型项目中非常不方便。
变量的默认初始化取决于编译器,你不应该依赖于它。在一个大型项目中,确保你的变量按你喜欢的方式初始化更有意义

,而不是取决于可能发生的事情。

您将int包装到一个轻量级且完全透明的类中,以优雅地解决这个问题,您有什么积极的经验?
by default,
which is very unconvenient in large projects. The default initialization of a variable is compiler dependent and you
should not depend on it. In a large project, it makes even more sense
to make sure your variables are initialized the way you like it, rather
than depend on what ''might'' happen
What is your positive experience in wrapping int to a lightweight and
completely transparent class which elegantly solves this problem?




I通常确保显式初始化变量。如果

它是一个类变量,我在类构造函数中执行,否则如果它是一个

局部变量,我会一起执行它。

我不会亲自把它包装成一个轻量级的。如果你试图做java所做的事情(每个基元有一个包装类

类型),那么再考虑一下在java中有一个包装类的要求

不同。



I usually make sure that the variables are initialized explicitly. If
it a class variable, I do it in the class constructor, else if it is a
local variable I do it along with declaration.
I would not wrap it into a lightweight class personally. If you are
trying to do what java does (have a wrapper class for each primitive
type), think again as the requirement to have a wrapper class in java
is different.


" Vladimir" < VL ********* @ yahoo.com>在留言中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...
"Vladimir" <vl*********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
我有点担心int类型没有被初始化为零
默认情况下,
这在大型项目中非常不方便。
I''m a little bit worried about type int not being initialized to zero
by default,
which is very unconvenient in large projects.




你能给出一个说明你关注的例子吗?



Can you give an example that illustrates your concern?


> int默认情况下没有被初始化为零


我建议你查看你的编译器的警告标志 - 它可能是

生成大多数未初始化的读取警告数据。此外,

是像Valgrind这样的工具,可以净化并确保这些问题,以及其他问题。这是一个更通用的解决方案,你建议的是什么。


更一般地,当你准备初始化时尝试定义变量

他们,并且对你的构造者非常严格,你应该没事。

我理解以你b $ b描述的方式包装int的肤浅吸引力,但是它过于古怪和侵略性。你会发现一些小问题

,例如模板被实例化为int和你的int包装器

类(次要代码膨胀),traits类无法工作(例如某人

写一个专门用于int的模板,但是你传递了一个

Int<>)等... /


Tony

> int not being initialized to zero by default

I suggest you check your compiler''s warning flags - it can probably
generate warning on most reads of uninitialised data. Further, there
are tools like Valgrind, purify and insure that such issues, as well as
myriad other issues. This is a more general solution that what you
propose.

More generally, try to define variables when you''re ready to initialise
them, and be rigourous with your constructors, and you should be fine.
I understand the superficial appeal of wrapping int in the way you
describe, but it''s too quirky and invasive. You''ll find minor issues
such as templates being instantiated for both int and your int wrapper
class (minor code bloat), traits classes failing to work (e.g. someone
writes a template that is specialised for int, but you pass it an
Int<>) etc.../

Tony


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

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