1

I'm trying to modify a bool field in a method. The method accepts a pointer pointer bool, but can't seem to figure it out how to do this correctly.

This is a basic example of something similar I want to do:

class WarningManager {
    bool seenWarningA; 

    void updateWarnings() {
        pushWarning(&seenWarningA)
    }

    void pushWarning(bool ** warning) {
        **warning = true;
    }
} 

This code example errors (sending bool* but needs to be bool**) and I've tried other ways with no luck. Can't find any online examples but maybe I'm searching for the wrong terms. What is the right way to do this?

6
  • 2
    Why do you think you need to use pointers here? Commented Sep 2, 2018 at 23:33
  • What do you think warning points to? The address of seenWarningA? Where do you think that address is stored? Commented Sep 2, 2018 at 23:39
  • @NeilButterworth If you can think of a better way of doing this without pointers, I would also be interested. Could do ref pointers? Commented Sep 2, 2018 at 23:39
  • 1
    I see no need for double-indirection in this code at all. At-most single indirection is suitable (or just a regular reference and no pointers). I can only assume pushWarning is doing something monumental an unseen; otherwise this is pointless and updateWarnings would simply seenWarningsA = true; would be done. Commented Sep 2, 2018 at 23:41
  • I can't provide a better way because you have not really explained fully what you are trying to do. Whatever it is, the chances of it requiring a pointer-to-pointer are pretty minimal. Commented Sep 2, 2018 at 23:43

3 Answers 3

1

Since you have a class, no parameters are required.

class WarningManager {
bool seenWarningA; 

void updateWarnings() {
    pushWarning()
}

void pushWarning() {
    seenWarningA = true;
}
} 

Using references rather than pointers is more elegant.

class WarningManager {
bool seenWarningA; 

void updateWarnings() {
    pushWarning(seenWarningA)
}

void pushWarning(bool & warning) {
    warning = true;
}
}

If you want to use pointers, the & operator just gives single pointer rather than a double point:

class WarningManager {
bool seenWarningA; 

void updateWarnings() {
    pushWarning(&seenWarningA)
}

void pushWarning(bool * warning) {
    *warning = true;
}
} 
Sign up to request clarification or add additional context in comments.

3 Comments

Ah, I didn't realise the bottom example is all I need. I got confused in something I read. Thanks.
The bottom example isn't what you need, the second example is what you need. Notice when you're using pointers how confusing code becomes, and it's easy to mis-write it. That never happens with references, so it's far better to just use those (plus you wouldn't need to check for nullptr before use)
Yes, I totally agree Tas. I don't use pointers until I need to.
1

You appear to be trying to pass an argument of bool* into a function that takes bool**. Remove one of the layers of indirection from the parameter list, or add one to the value you're passing in. Either should work.

Comments

1

Two mistakes:

First- your declaration of pushWarning is with parameter of type bool**, and you are trying to send bool*.

Second- you can simply use reference:

using namespace std;

class WarningManager {
public:
    bool seenWarningA;

    void updateWarnings() {
        pushWarning(seenWarningA);
    }

    void pushWarning(bool &warning) { // You can simply use refference instead of pointer to pointer, or pointer at all..
        warning = true;
    }
};

int main()
{
    WarningManager w;
    w.seenWarningA = false;
    w.updateWarnings();
    cout << w.seenWarningA; // Prints 1

    return 0;
}

1 Comment

@Tas I took the name before it been edited, so I didn't had much attention to class's name, but you right. And I prefer to use using namespace std, I just forgot to put it here :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.