为什么默认情况下C ++ bool var为true?

时间:2023-01-30 00:42:39

bool "bar" is by default true, but it should be false, it can not be initiliazied in the constructor. is there a way to init it as false without making it static?

bool“bar”默认为true,但它应该为false,它不能在构造函数中初始化。有没有办法将它初始化为假而不使其静态?

Simplified version of the code:

简化版代码:

foo.h

class Foo{
 public:
     void Foo();
private:
     bool bar;
}

foo.c

Foo::Foo()
{  
   if(bar)
   {
     doSomethink();
   }
}

7 个解决方案

#1


63  

In fact, by default it's not initialized at all. The value you see is simply some trash values in the memory that have been used for allocation.

实际上,默认情况下它根本没有初始化。您看到的值只是内存中用于分配的一些垃圾值。

If you want to set a default value, you'll have to ask for it in the constructor :

如果要设置默认值,则必须在构造函数中请求它:

class Foo{
 public:
     Foo() : bar() {} // default bool value == false 
     // OR to be clear:
     Foo() : bar( false ) {} 

     void foo();
private:
     bool bar;
}

UPDATE C++11:

If you can use a C++11 compiler, you can now default construct instead (most of the time):

如果你可以使用C ++ 11编译器,你现在可以默认构造(大部分时间):

class Foo{
 public:
     // The constructor will be generated automatically, except if you need to write it yourself.
     void foo();
private:
     bool bar = false; // Always false by default at construction, except if you change it manually in a constructor's initializer list.
}

#2


4  

Klaim's answer is spot on. To "solve" your problem you could use a constructor initialization list. I strongly suggest you read that page as it may clear up some similar queries you may have in future.

克莱姆的回答很明显。要“解决”您的问题,您可以使用构造函数初始化列表。我强烈建议您阅读该页面,因为它可能会清除您将来可能遇到的一些类似问题。

#3


1  

C / C++ don't initialize variables for you at all. The memory location which is now in use by bar had a value in it which is interpreted as "true". This will not always be the case. You must initialize it in your constructor.

C / C ++根本不为你初始化变量。现在由bar使用的内存位置在其中具有一个值,该值被解释为“true”。情况并非总是如此。您必须在构造函数中初始化它。

#4


1  

This program has undefined behavior. Intrinsic types do not have constructors. You could do bool bar = bool(); but it's better to define the actual value in your foo class.

该程序具有未定义的行为。内在类型没有构造函数。你可以做bool bar = bool();但最好在你的foo类中定义实际值。

#5


0  

Since you're using the value of bar in the ctor, I assume you're trying to set it before the object is created? I think you meant to make it a static class variable instead of an instance variable.

既然您在ctor中使用了bar的值,我假设您在创建对象之前尝试设置它?我认为你的意思是使它成为静态类变量而不是实例变量。

#6


0  

Try this:

class Foo
{ 
  public: 
    void Foo(); 
  private: 
     bool bar; 
} 

Foo::Foo() : bar(false)
{
  if(bar) 
  { 
    doSomething(); 
  }
}

#7


-1  

You can initialize any variable when you declare it.

您可以在声明任何变量时对其进行初始化。

bool bar = 0;

#1


63  

In fact, by default it's not initialized at all. The value you see is simply some trash values in the memory that have been used for allocation.

实际上,默认情况下它根本没有初始化。您看到的值只是内存中用于分配的一些垃圾值。

If you want to set a default value, you'll have to ask for it in the constructor :

如果要设置默认值,则必须在构造函数中请求它:

class Foo{
 public:
     Foo() : bar() {} // default bool value == false 
     // OR to be clear:
     Foo() : bar( false ) {} 

     void foo();
private:
     bool bar;
}

UPDATE C++11:

If you can use a C++11 compiler, you can now default construct instead (most of the time):

如果你可以使用C ++ 11编译器,你现在可以默认构造(大部分时间):

class Foo{
 public:
     // The constructor will be generated automatically, except if you need to write it yourself.
     void foo();
private:
     bool bar = false; // Always false by default at construction, except if you change it manually in a constructor's initializer list.
}

#2


4  

Klaim's answer is spot on. To "solve" your problem you could use a constructor initialization list. I strongly suggest you read that page as it may clear up some similar queries you may have in future.

克莱姆的回答很明显。要“解决”您的问题,您可以使用构造函数初始化列表。我强烈建议您阅读该页面,因为它可能会清除您将来可能遇到的一些类似问题。

#3


1  

C / C++ don't initialize variables for you at all. The memory location which is now in use by bar had a value in it which is interpreted as "true". This will not always be the case. You must initialize it in your constructor.

C / C ++根本不为你初始化变量。现在由bar使用的内存位置在其中具有一个值,该值被解释为“true”。情况并非总是如此。您必须在构造函数中初始化它。

#4


1  

This program has undefined behavior. Intrinsic types do not have constructors. You could do bool bar = bool(); but it's better to define the actual value in your foo class.

该程序具有未定义的行为。内在类型没有构造函数。你可以做bool bar = bool();但最好在你的foo类中定义实际值。

#5


0  

Since you're using the value of bar in the ctor, I assume you're trying to set it before the object is created? I think you meant to make it a static class variable instead of an instance variable.

既然您在ctor中使用了bar的值,我假设您在创建对象之前尝试设置它?我认为你的意思是使它成为静态类变量而不是实例变量。

#6


0  

Try this:

class Foo
{ 
  public: 
    void Foo(); 
  private: 
     bool bar; 
} 

Foo::Foo() : bar(false)
{
  if(bar) 
  { 
    doSomething(); 
  }
}

#7


-1  

You can initialize any variable when you declare it.

您可以在声明任何变量时对其进行初始化。

bool bar = 0;