【问题标题】:problems declaring bool false in class在类中声明 bool false 的问题
【发布时间】:2014-11-05 03:08:58
【问题描述】:
boolean.cpp:
Boolean::Boolean() : test1(false),test2(false)
{
}

void Boolean::exec() {
  test1 = true;
  test2 = true;
  if ((!test1) && (!test2))
     std::cout << "both test1 && test2 are false" << std::endl;
  else
     std::cout << "test1 is " << test1 << " test2 is " << test2 << std::endl;
}

void Boolean::exec2() {
  if ((!test1) && (!test2))
     std::cout << "both test1 && test2 are false" << std::endl;
 else
     std::cout << "test1 is " << test1 << " test2 is " << test2 << std::endl;
}

boolean.h:
class Boolean {
private:
  bool test1;
  bool test2;

public:
  Boolean();
  void exec();
  void exec2();
};

main.cpp:
int main(int argc, char *argv[]) {
Boolean start;
start.exec();
Boolean start2;
start2.exec2();
}

输出:

test1 is 1 test2 is 1
both test1 & test2 are false

如果我在开始时使用默认构造函数将 test1 和 test2 设置为 false。 如果我需要一个新的 Boolean 实例,在 Boolean::exec() 中设置的值会被覆盖。

bool test1 = false;类中不允许声明。 如果没有默认构造函数,则布尔值不会被初始化。

那么声明 bool 'false' 并在设置时保持 'true' 的最佳解决方案是什么?

【问题讨论】:

  • 老实说,我承认这个问题对我没有任何意义。是否只想使用 Boolean 的单个实例?
  • 我不太明白你想要达到什么目的;您可以引入一个以两个 bool 作为参数的构造函数,以便您可以在创建时指定它们。 exec1 更改相应实例的状态,exec2 不会。请提供有关所需行为的更多背景信息。
  • Boolean 的第二个实例只是为了再次调用默认构造函数。在一个常见的项目中,我会在另一个 .cpp 文件中使用另一个 Boolean 实例,例如我想要实现的是:在开始时将 bool 设置为 false,如果在 .txt 文件中找到一个字符串,将 bool 设置为 true,另一个类的方法检查 true 或 false 并进行适当的计算
  • @12dollar 仍然没有意义。暂时忘记此代码。描述你试图解决的问题然后描述你是如何希望这个代码可以解决这个问题的,可悲的是失败了。如果您期望 Boolean 的第二个实例化以某种方式改变一个不相关的实例,那么如果没有静态成员就不会发生这种情况(老实说这毫无意义)。
  • @12dollar 如果exec2()Boolean 的同一实例上调用,则为true。每个实例(对象)都有自己的非静态成员副本。您是否尝试在对象之间共享数据?如果是这样,为什么?可以详细说一下吗?

标签: c++ boolean declare


【解决方案1】:

您正在声明两个布尔实例,它们占用两个不同的内存位置,因此您遇到的是正常行为

如果您希望两个实例共享变量,则将变量声明为静态

布尔值.h:

class Boolean {
private:
  static bool test1;
  static bool test2;

中定义它们

boolean.cpp

Boolean::test1 = false;
Boolean::test2 = false;

编辑:请注意,所有 Boolean 实例现在都将共享这些变量。

【讨论】:

  • 这是我唯一能理解的 OP 也试图完成的事情。
  • 是的,谢谢,这就是我一直在努力实现的目标。很抱歉误导我还为时过早。我已经尝试将 bool 设置为 static 但由于将它们添加到 Boolean::exec() 函数中而无法编译。应该从一开始就在外面宣布它们。干杯!
【解决方案2】:

我假设您不想默认使用 'false' 初始化它们,但它们应该有一些默认值。

您可以使用默认参数ctor,以便在创建新实例时,可以选择设置值,如果未设置,则将设置默认值。

// cotr

Boolean(bool test1Val = false, bool test2Val = false);

// ctor implementaion

Boolen::Boolean(bool test1Val, bool test2Val) {
test1 = test1Val;
test2 = test2Val;
}

【讨论】:

  • 在构造函数初始化列表中初始化成员变量是一个好习惯,而不是在构造函数的主体中。
【解决方案3】:

您观察到的行为是完全定义的。您为您的类Boolean 提供一个默认构造函数,该构造函数将test1test2 两个属性都初始化为false。

Boolean start; // -> calls Boolean::Boolean() thus start.test1 == start.test2 == false
start.exec(); // Sets start.test1 and start.test2 to true
Boolean start2; // -> calls Boolean::Boolean() thus 
                // start2.test1 == start2.test2 == false
start2.exec2(); // Nothing done to start2.test1/2 => they are still false.

我认为您希望能够编辑内部属性(setter 方法)。您不妨定义另一个以两个布尔值作为参数的构造函数。

这是你的类的明显设置方法:

void Boolean::setTests(bool t1, bool t2) {
    test1 = t1;
    test2 = t2;
}

在你的主要功能中你只需要做:

Boolean start;
start.setTests(true, false); // or whatever

【讨论】:

    【解决方案4】:

    如果您想将 test1 和 test2 初始化为 false,则在一个布尔实例中将它们设置为 true,但不要在新的布尔实例中将它们重置为 false,这意味着您需要静态变量。

    boolean.h:
    class Boolean {
    private:
        static bool test1;
        static bool test2;
    
    public:
        Boolean();
        void exec();
        void exec2();
    };
    
    boolean.cpp:
    bool Boolean::test1 = false;
    bool Boolean::test2 = false;
    
    void Boolean::exec() {
        Boolean::test1 = true;
        Boolean::test2 = true;
        if ((!test1) && (!test2))
            std::cout << "both test1 && test2 are false" << std::endl;
        else
            std::cout << "test1 is " << test1 << " test2 is " << test2 << std::endl;
    }
    
    void Boolean::exec2() {
        if ((!test1) && (!test2))
            std::cout << "both test1 && test2 are false" << std::endl;
        else
            std::cout << "test1 is " << test1 << " test2 is " << test2 << std::endl;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-12
      • 2022-01-20
      • 2011-03-15
      • 1970-01-01
      • 2011-06-11
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多