在课堂上宣布bool假的问题

时间:2014-09-11 08:37:25

标签: c++ boolean declare

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值。

那么宣布bool'false'并保持'true'的最佳解决方案是什么?

4 个答案:

答案 0 :(得分:2)

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

如果希望两个实例共享变量,则声明变量static

boolean.h:

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

中定义它们

boolean.cpp

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

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

答案 1 :(得分:0)

我假设你不想用&#39; false&#39;来初始化它们。默认情况下,它们应该有一些默认值。

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

// cotr

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

// ctor implementaion

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

答案 2 :(得分:0)

您正在观察的行为是完美定义的。您为类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方法)。你也可以定义另一个带有两个布尔值作为参数的构造函数。

以下是您班级明显的setter方法:

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

在您的主要功能中,您只需要这样做:

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

答案 3 :(得分:0)

如果要将test1和test2初始化为false,则在一个Boolean实例中将它们设置为true,但不要在新的Boolean实例中将它们重置为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;
}
相关问题