0
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();
}

output:

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

if I use a default constructor to set test1 and test2 to false at start. the values set in Boolean::exec() get overwritten if I need a new instance of Boolean.

bool test1 = false; declaration is not allowed in a class. without default constructor the bool values are not initialized.

so what's the best solution to declare bool 'false' and keep 'true' if it's set ?

  • 2
    I honestly admit the question doesn't make any sense to me. Do you want to use just a single instance of `Boolean`? – raina77ow Sep 11 '14 at 08:41
  • I don't quite understand what you are trying to achieve; you could introduce a constructor which takes two `bool` as argument so you can specify them on creation. `exec1` changes the state of the respective instance, `exec2` does not. Please provide more context on what the desired behaviour is. – Codor Sep 11 '14 at 08:42
  • the second instance of Boolean was just for calling the default constructor again. in a common project I'd use another instance of Boolean in an other .cpp file e.g. what I'm trying to achieve is something like: set bool to false at start, if a string is found in a .txt file set bool to true, a method of an other class checks for true or false and does the appropriate calculations –  Sep 11 '14 at 08:44
  • 1
    @12dollar still makes no sense. Forget this code for a minute. Describe the *problem* you're trying to solve, *then* describe how you had *hoped* this code would address it, and woefully fails. if you're expecting a second instantiation of `Boolean` to somehow alter an unrelated instance, that isn't going to happen without static members (which honestly would be pointless). – WhozCraig Sep 11 '14 at 08:45
  • Setting non-const member variables in class declaration is not supported until C++11. You need to use a constructor to initialise the values. The `keep true if it's set` is completely unclear to me. – W.B. Sep 11 '14 at 08:47
  • yeah sorry for being misleading. 'keep true if it's set' should mean if Boolean::exec() sets test1 to true it should be true if Boolean::exec2() is called too –  Sep 11 '14 at 08:48
  • 2
    @12dollar It will be true if `exec2()` is called *on the same instance of `Boolean`*. Each instance (object) has its own copy of non-static members. Are you trying to share data between objects? If so, why? Can you give details? – Angew is no longer proud of SO Sep 11 '14 at 08:50
  • There is obviously a confusion with instance attributes and class attributes. – Rerito Sep 11 '14 at 08:52

4 Answers4

2

You are declaring two instances of Boolean, these occupy two different memory locations so what you are experiencing is normal behavior

If you want the two instances to share variables then declare the variables static

boolean.h:

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

an define them in

boolean.cpp

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

EDIT: note that all instances of Boolean will now share these variables.

Community
  • 1
  • 1
AndersK
  • 35,813
  • 6
  • 60
  • 86
  • 1
    This is the only thing I could fathom the OP is trying to accomplish as well. – WhozCraig Sep 11 '14 at 08:50
  • yeah thanks that's what I've been trying to achieve. sorry for being misleading still too early for me. I already tried setting bool to static but weren't able to compile due to adding them into the Boolean::exec() function. should have declared them outside from the start. cheers ! –  Sep 11 '14 at 08:59
0

I assume you do not want to initialize them with 'false' by default but they should have some default values.

You can use default argument ctor so that when new instance is created, have an option of setting value, if not set, then default values will be set.

// cotr

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

// ctor implementaion

Boolen::Boolean(bool test1Val, bool test2Val) {
test1 = test1Val;
test2 = test2Val;
}
Koteswara sarma
  • 434
  • 3
  • 7
  • It's a good habit to initialise member variables in constructor initializer lists, rather then constructor's body. – W.B. Sep 11 '14 at 08:49
0

The behaviour you are observing is perfectly defined. You supply a default constructor for your class Boolean that initializes to false both attributes test1 and test2.

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.

I think you want to be able to edit the inner attributes (a setter method). You might as well define another constructor that takes two boolean as arguments.

Here is the obvious setter method for your class :

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

In your main function you just have to do :

Boolean start;
start.setTests(true, false); // or whatever
Rerito
  • 5,886
  • 21
  • 47
0

If you want to test1 and test2 to be initialized to false, then set them to true in one Boolean instance, but not reset them to false in a new Boolean instance, that means you want static variables.

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;
}