Confused about allocating listeners via pointer


#1

Hello all.

I am attempting to add a listener to a seperate class from the component that holds a button.

I declare a private button and a pointer to it in the component:

TextButton changeButton;
Button * changeButtonPtr = &changeButton;

I then declare a public function to return the pointer:

Button * Gui::getChangeButtonPtr()
{
    return changeButtonPtr;
}

In the listening class I get the button pointer via the class object pointer and declare a listener:

changeButtonPtr = guiPtr->getChangeButtonPtr();
changeButtonPtr->addListener(this);


Add the code to test:

void Hub::buttonClicked (Button* button)
{
    if (button == changeButtonPtr)
    {
        cout << "Working?" << endl;
    }
}

And we have a great steaming pile of nada.

Thank you anyone for your help and explaination, apologies for the beginner questions!

 

 

 


#2

What do you mean by nada? Are you seeing the button for instance? I can't see any code where you call addAndMakeVisible...

Also, why not add the TextButton as ScopedPointer<TextButton>. No need to have both the TextButton and a pointer to it.


#3

The button appears fine, it's that there's something wrong with the code for setting the listener.

I will experiment with that when I get it working, because I'm just starting out I tend to go through things the well-trodden way until I get them to work properly and then experiment with new things!


#4

Mmm, the well trodden way is to use ScopedPointers.

Can you post more of your code? Otherwise it's a bit of a guess for us to figure out what's going wrong.


#5

It's just the basic way I learned from the tutorials, I have to move out from it with very small steps!

That's the entire code to do with the listener, and I can confirm that the button is broadcasting when a tutorial typical listener is placed in the same class.


#6

Ok, is your Hub class inheriting from Button::Listener?


#7

It is indeed, thankfully the compiler usually catches that one!


#8

Is the Hub object getting constructed before the TextButton?


#9

No, after. Could this be the source of the problems? I think I may have been trying to run before walking by altering the structure of my program. It runs two classes seperately from the MainComponent (to seperate the gui from the engine as it were) rather than the engine being called from the gui as the default setup does.


#10

If you are assigning changeButtonPtr in your class constructors then Hub should happen after Gui, otherwise changeButtonPtr will be null when Hub accesses it.

I'm not sure I can help much more without seeing more of your code.


#11

I've checked that it's all in order. It looks like you can't add listeners via pointers. I think I may change back to the simple way of doing it (calling functions in the other class from listeners in the same class as the component. Hopefully I'll understand it a bit better in the future and understand if / where I went wrong.

Thank you very much for your help Andrew, made me think in some new ways to work things out.


#12

Yes you can… You just have to make sure the object scope and lifetime is correct.

Your code didn’t show where you’re creating the object… it simply showed you have a pointer.

Somewhere you need a ScopedPointer to manage the lifetime of the object… and you can share that pointer value between anything… as long as you make sure the pointer lifetime is valid and you don’t use it if it’s not.

Rail


#13

I re-rigged everything and discovered null pointers, those sneaky devils. I'm presuming this is a typical beginner problem? One would think it's an obvious thing, but they are so easy to slip by unnoticed.

Adding listeners by pointer now works, and my code is cleaner now due to declaring components with Scoped Pointers.

Thank you all for your help and food for thought, and apologies for the noobish questions and ramblings!


#14

Probably safe to say that if you've not come across a problem with null pointers, then you've not done any C++ coding :)