Const Modifier and ofxGui Sliders

Hi folks. This is more of a C++ question than anything, but it involves the ofxGui library.

I’d like to wrap a collection of ofxSlider objects in a class call Parameters. I’d then like an object of type Parameteres to be stored as a reference within a number of other objects. (The idea being to allow a bunch of objects to pull tweakable parameters from a shared source.)

Things were working fine until I decided that I wanted to ensure const correctness by storing the references to the Parameters object as a const reference. This meant that the Parameters slider-getters had to be const, which is turn caused the compiler to complain about their being no suitable conversion between const ofxIntSlider to int.

Here’s a simplified implementation of the Parameters h:

#include "ofxGui.h"

class Parameters {
	
	ofxPanel gui_panel;
	ofxIntSlider avoidance_multiplier;
	
public:
	Parameters();
	void draw();
	int get_avoidance_multiplier() const;
};

And the associated class implementation:

#include "Parameters.h"

Parameters::Parameters() {
	gui_panel.setup();
	gui_panel.add(avoidance_multiplier.setup("Avoidance Multiplier", 4, 0, 15));
}

void Parameters::draw() {
	gui_panel.draw();
}

int Parameters::get_avoidance_multiplier() const {
	// ### THIS IS THE PROBLEM LINE:
	return avoidance_multiplier; // Works fine when function isn't const.
}

Can someone explain to me what I’d need to do to convert avoidance_multiplier, which is now a const ofxSlider, into the int stored within it? Your help is much appreciated.

this sounds like a bug, dereferencing a slider to it’s value should probably be const. can you open a github issue explianing this?

Also if you use ofParameter<int> instead of the slider directly it’ll surely allow you to do this and is the recomended way to use ofxGui, take a look at the guiFromParametersExample

Wow @arturo thanks for the super quick response!

You are correct, it works without issue when I use ofParameter instead of ofxSlider.

I will open a github issue about ofxSlider dereferencing in a const unfriendly manner. :slight_smile: