Automatic object initialization

People, I´m trying to use auto obj initialization but something is wrong with addAndMakeVisible().

working code

Slider* sl1; sl1 = new Slider(""); sl1->setSliderStyle (Slider::Rotary); sl1->setBounds (10, 100, 80, 80); addAndMakeVisible(sl1);

NOT working code

Slider sl1; sl1.setSliderStyle (Slider::Rotary); sl1.setBounds (10, 100, 80, 80); addAndMakeVisible(sl1);

NOT working code too

Slider sl1; sl1.setSliderStyle (Slider::Rotary); sl1.setBounds (10, 100, 80, 80); addAndMakeVisible(&sl1);

Any idea how to fix the code ? thanks.

Your last “section” of code is working. You just don’t see your slider because it goes out of scope!

Ok thanks, I just learn about the objects life time.