English 中文(简体)
C++中未加工的养蜂的价值? [复制]
原标题:Default value of an unset boolean in C++? [duplicate]
  • 时间:2011-10-23 02:24:02
  •  标签:
  • c++
  • boolean
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Why is a C++ bool var true by default?

我就是这样做的:

class blah
{
  public:
  bool exampleVar;
};

blah exampleArray[4];
exampleArray[1].exampleVar = true;

例如,Array,现在有3起不定的例子,即Var,在我没有说明他们的违约价值是什么?

最佳回答

缺省值取决于<代码>exampleArray的申报范围。 如果是地方性的,那么这些价值观将是随机的,那么不管这些 st点的数值如何。 如果该数值是静态的,或者在档案范围(全球)上宣布,则数值为零。

Here s示范。 如果你需要一个成员变量,以具有决定性价值,则总是在施工者中开始。

class blah
{
  public:
  blah() 
  : exampleVar(false)
  {}

  bool exampleVar;
};

EDIT:
The constructor in the above example is no longer necessary with C++11. Data members can be initialized within the class declaration itself.

class blah
{
  public:
  bool exampleVar = false;
};

This inline default value can be overridden by a user-defined constructor if desired.

问题回答
struct x
{
    bool b;

    x() : b() { }
}

...
x myx;

在本案中,我的手脚将是错误的。

struct x
{
    bool b;
}

...
x myx;

in this case, myx.b will be unpredictable, it will be the value that location of memory had before allocating myx.

由于在C和C++中,虚假价值被定义为0,真实价值被定义为非零,因此,任意地址所在地将包含真实价值而不是虚假价值的可能性更大。 通常在C++中,面积为1,即8倍。 超过255种可能性是,随机存留地点是假的,这解释了为什么你认为违约的风气价值是真实的。

它们的违约价值没有界定。 你们的帽子取决于他们被划为一件事,常常被称作“垃圾”。

视您的汇编者而定,可将其定为<代码>false。 但即便如此,你也更难以确定。

不确定。

非法定成员变量必须先入门,除非您能够保证对其进行的第一次操作是书面的。 最常见的方法是通过建筑商。 如果您仍然希望有一个无动于衷的构造:

public:
blah() : exampleVar(false) {}

缺省值为indeterminate。 每当你管理你的节目时,都可能不同。 你们应先把价值推到某种东西,或者有另一个变数,表明贵国的私人成员不是初步的。

@Praetorian: 他回答中的要点。

但也值得注意。

blah exampleArray[4];         // static storage duration will be zero-initialized 
                              // ie POD member exampleVar will be false.

void foo()
{
    blah exampleArray1[4];    // automatic storage duration will be untouched.
                              // Though this is a class type it only has a compiler 
                              // generated constructor. When it is called this way 
                              // it performs default-construction and POD members
                              // have indeterminate values


    blah exampleArray2[4] = {};  // Can force zero-in initialization;
                                 // This is list-initialization.
                                 // This forces value-initialization of each member.
                                 // If this is a class with a compiler generated constrcutor
                                 // Then each member is zero-initialized


    // following through to non array types.
    blah       tmp1;            // Default initialized: Member is indeterminate
    blah       tmp2 = blah();   // Zero Initialized: Members is false

    // Unfortunately does not work as expected
    blah       tmp3();          // Most beginners think this is zero initialization.
                                // Unfortunately it is a forward function declaration.
                                // You must use tmp2 version to get the zero-initialization 
}

C/C++没有界定未签署的违约数值。 如果你需要某种特定价值,那么就会产生一种制造装置,确定你的违约价值。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签