复制默认构造函数的“非初始化状态” [英] replicating default constructor's "non-initializing state"

查看:77
本文介绍了复制默认构造函数的“非初始化状态”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

我有一个简单的结构,比如颜色(R,G和B)。我

创建了我自己的构造函数来简化它的创建。结果,我失去了

的默认构造函数。我不喜欢这个,但它很容易解决:我

只是制作我自己的默认构造函数。


问题:

我自己的默认构造函数被认为是*初始化

变量*(即使它没有),而原始的构造函数则不是* b $ b。因此,当我在初始化之前声明并使用它时,

编译器不再警告我。


问题:

是有任何编译器设置(甚至编译器特定的;我是使用MSVC ++的
)状态我的默认构造函数的行为与

常规默认构造函数完全相同吗?


感谢您的时间,

Jason


PS我的默认构造函数可以将变量初始化为全0',

但这有两个不需要的效果:1。它减慢了代码,其中这个初始化不需要'b $ b b'发生了。 2.它可能隐藏了如果它未被初始化而将会显示出来的错误。 (这是
类似于MSVC ++的调试器如何将所有变量初始化为0,其中
是愚蠢的,因为它应该将它们初始化为随机性 - 就像 - 使错误看起来像

一样快。

解决方案

< blockquote> Jason Doucette写道:


情况:

我有一个简单的结构,比方说,它有颜色(R,G和B)。我

创建了我自己的构造函数来简化它的创建。结果,我失去了

的默认构造函数。我不喜欢这个,但它很容易解决:我

只是制作我自己的默认构造函数。


问题:

我自己的默认构造函数被认为是*初始化

变量*(即使它没有),而原始的构造函数则不是* b $ b。因此,当我在初始化之前声明并使用它时,

编译器不再警告我。



这是你的选择,不是吗?你选择以这样的方式实现你的c-tor

(应该初始化成员)

*不*做它所承诺的做。所以,你为什么抱怨?这不是问题,或者至少它很容易解决,不是吗?


>

问题:

是否有任何编译器设置(甚至编译器特定的设置;我是使用MSVC ++的
)状态我的默认构造函数的行为完全像

常规默认构造函数?



不是我所知道的。


>

谢谢你你的时间,

杰森


PS我的默认构造函数可以将变量初始化为全0',

但这有两个不需要的效果:1。它减慢了代码,其中这个初始化不需要'b $ b b'发生了。



你能分享这些数字吗,它实际上要多少* *减慢

代码"?
< blockquote class =post_quotes>
2.如果它未被初始化,它可能会隐藏

会出现的错误。



写得正确*它_prevents_错误,而不是隐藏它们。


(这是

类似于MSVC ++的调试器如何将所有变量初始化为0,这对于
来说是愚蠢的,因为它应该将它们初始化为随机性 - 这将是

在发布版本中发生 - 使错误看起来像

一样快。



完全不相似。当处于调试状态时构建变量给出了一些值,而在非调试中则给出了一些值。他们不是,如果您的代码依赖于此,那么您有一个很大的问题。当你的默认

c-tor初始化成员变量(为0或其他)*总是*,那么

是无随机性的,你可以*依赖*初始化发生。


V

-

请在通过电子邮件回复时删除资金'A'

我没有回复最热门的回复,请不要问


2008年4月7日星期一12:07:03 -0700( PDT),Jason Doucette

< jd ******* @ gmail.comwrote:


>情况:
问题:
我自己的默认构造函数被认为是*初始化
变量*(即使它不是),而原始的不是。因此,当我在初始化之前声明并使用它时,
编译器不再警告我。

问题:
是否有任何编译器设置(甚至是特定于编译器的设置;我是
使用MSVC ++)状态我的默认构造函数的行为与常规默认构造函数完全相同?



No.


> P.S。我的默认构造函数可以将变量初始化为全0',但这有两个不需要的效果:1。它减慢了不需要进行初始化的代码。



几乎可以肯定是在一个难以察觉的程度。


> 2。它可能隐藏了如果它未被初始化而将会出现的错误。 (这类似于MSVC ++的调试器将所有变量初始化为0,这很傻,因为它应该将它们初始化为随机性 - 在发布版本中会发生 - - 尽可能快地出现错误)。



调试器不会这样做,而AFAIK从来没有。 (多年来我一直听到这个

,我仍然不知道这个谣言是如何开始的。)当

某些调试选项生效时,编译器会将本地化初始化为

某些非零模式。


-

Doug Harrison

Visual C ++ MVP


On 2008-04-07 15:19:10 -0400," Victor Bazarov" < v。******** @ comAcast.netsaid:


Jason Doucette写道:


>问题:
是否有任何编译器设置(甚至编译器特定的;我使用MSVC ++)状态我的默认构造函数的行为与常规默认构造函数完全相同?



我不知道。



未来,C ++ 0x会让你这样做:


struct S

{

S()=默认;

S(int);

};


,结果是编译器会生成默认的

构造函数,好像你没有声明另一个。


-

Pete

Roundhouse Consulting,Ltd。( www。 versatilecoding.com

标准C ++库扩展:一个教程和参考的作者
www.petebecker.com/tr1book


Situation:
I have a simple struct that, say, holds a color (R, G, and B). I
created my own constructors to ease its creation. As a result, I lose
the default constructor. I dislike this, but it''s easy to solve: I
just make my own default constructor.

Problem:
My own default constructor is considered to be *initializing the
variable* (even though it doesn''t), whereas the original one does
not. Thus, when I declare and use it before initializing it, the
compiler no longer warns me.

Question:
Are there any compiler settings (even compiler specific ones; I am
using MSVC++) that state MY default constructor behaves exactly like
the regular default constructor?

Thanks for your time,
Jason

P.S. My default constructor could initialize the variable to all 0''s,
but this has two unwanted effects: 1. It slows down code in which
this initialization needn''t occur. 2. It potentially hides bugs that
would show up if it were left uninitialized as it should be. (This is
similar to how MSVC++''s debugger initializes all variables to 0, which
is silly, since it should initialize them to randomness -- as will
happen in the release build -- to make bugs appear as quickly as
possible).

解决方案

Jason Doucette wrote:

Situation:
I have a simple struct that, say, holds a color (R, G, and B). I
created my own constructors to ease its creation. As a result, I lose
the default constructor. I dislike this, but it''s easy to solve: I
just make my own default constructor.

Problem:
My own default constructor is considered to be *initializing the
variable* (even though it doesn''t), whereas the original one does
not. Thus, when I declare and use it before initializing it, the
compiler no longer warns me.

That''s your choice, isn''t it? You''ve chosen to implement your c-tor
(which is supposed to initialise the members) in such a way that does
*not* do what it promises to do. So, why are you complaining? It is
not a problem, or at least it''s very easy to solve, isn''t it?

>
Question:
Are there any compiler settings (even compiler specific ones; I am
using MSVC++) that state MY default constructor behaves exactly like
the regular default constructor?

Not that I know of.

>
Thanks for your time,
Jason

P.S. My default constructor could initialize the variable to all 0''s,
but this has two unwanted effects: 1. It slows down code in which
this initialization needn''t occur.

Can you share the numbers, how much *does* it actually "slow down
the code"?

2. It potentially hides bugs that
would show up if it were left uninitialized as it should be.

Written *correctly* it _prevents_ bugs, not hides them.

(This is
similar to how MSVC++''s debugger initializes all variables to 0, which
is silly, since it should initialize them to randomness -- as will
happen in the release build -- to make bugs appear as quickly as
possible).

Not similar at all. When in the "debug" build variables are given
some values whereas in the "non-debug" they are not, you have a very
big problem if your code ever depends on this. When your default
c-tor initialises member variables (to 0 or whatever) *always*, there
is no randomness, and you can *rely* on the initialisation happening.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On Mon, 7 Apr 2008 12:07:03 -0700 (PDT), Jason Doucette
<jd*******@gmail.comwrote:

>Situation:
I have a simple struct that, say, holds a color (R, G, and B). I
created my own constructors to ease its creation. As a result, I lose
the default constructor. I dislike this, but it''s easy to solve: I
just make my own default constructor.

Problem:
My own default constructor is considered to be *initializing the
variable* (even though it doesn''t), whereas the original one does
not. Thus, when I declare and use it before initializing it, the
compiler no longer warns me.

Question:
Are there any compiler settings (even compiler specific ones; I am
using MSVC++) that state MY default constructor behaves exactly like
the regular default constructor?

No.

>P.S. My default constructor could initialize the variable to all 0''s,
but this has two unwanted effects: 1. It slows down code in which
this initialization needn''t occur.

Almost certainly by an imperceptible degree.

>2. It potentially hides bugs that
would show up if it were left uninitialized as it should be. (This is
similar to how MSVC++''s debugger initializes all variables to 0, which
is silly, since it should initialize them to randomness -- as will
happen in the release build -- to make bugs appear as quickly as
possible).

The debugger doesn''t do that, and AFAIK, never has. (I''ve been hearing this
for many years, and I still don''t know how this rumor got started.) When
certain debug options are in effect, the compiler will initialize locals to
certain non-zero patterns.

--
Doug Harrison
Visual C++ MVP


On 2008-04-07 15:19:10 -0400, "Victor Bazarov" <v.********@comAcast.netsaid:

Jason Doucette wrote:

>Question:
Are there any compiler settings (even compiler specific ones; I am
using MSVC++) that state MY default constructor behaves exactly like
the regular default constructor?


Not that I know of.

For the future, though, C++0x will let you do this:

struct S
{
S() = default;
S(int);
};

with the effect that the compiler will generate that default
constructor as if you hadn''t declared the other one.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


这篇关于复制默认构造函数的“非初始化状态”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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