views:

139

answers:

3

I'm getting said error on this line "b = true". Now Why am I getting this error? Aren't I pointing to TurnMeOn and thus saying TurnMeOn = true?

class B{
void turnOn(bool *b){b = true}
};

int main(){
B *b = new B();
bool turnMeOn = false;
b->turnOn(&turnMeOn);
cout << "b = " << turnMeOn << endl;
}
+4  A: 

No. As you've written it, it would need to be *b = true.

Alternatively, you could write the function to take a reference to a bool, so that

void turnOn(bool &b) { b = true; }

would be correct.

Mark Rushakoff
+8  A: 
b->turnOn(&turnMeOn);

and

   *b = true;
Anton
This worked. Thanks
William
You're welcome.
Anton
I'd like to point out that passing by pointer here makes the calling code more readable than passing by reference as some of the other comments suggest.
allenporter
+5  A: 

turnOn requires a pointer to bool as parameter. You're using it as an actual bool. I guess you're looking for a reference, i.e. bool& b as parameter declaration in your method.

Pieter
+1: In this case, the reference is probably the best way to go.
ereOn
This.Also, the OP forgot to delete his B when done.
DeadMG