0

For my GUI I have a class to add more functionality to Gtk::Entries. In that class I have a protected boolean variable. I have functions built to adjust the boolean variable to be true or false when it needs to be, this works no problem. When I try to pass that boolean value is where the problem occurs

For instance what did not work is:

((Gtk::Entry *)w)->signal_activate().connect(sigc::bind<Gtk::Widget *>(sigc::mem_fun(*this, &Dialog::function), w2));

The Dialog::function looks like this:

void Dialog::function(Gtk::Widget *w2)
{
    if((MyClass *) this)->get_boolean_val())
         w2->do_something();
}

Whenver I did this the boolean value would always come back false, even after varifying that the class variable would change. I proved that by editing the above as such.

((Gtk::Entry *)w)->signal_activate().connect(sigc::bind<Gtk::Widget *, Gtk::Entry *>(sigc::mem_fun(*this, &Dialog::function), w2, w));

and changing the function body to this:

void Dialog::function(Gtk::Widget *w2, Gtk::Entry *w)
{
    if((MyClass *) w)->get_boolean_val())
         w2->do_something();
}

This works but I feel as if there is a better way to edit the 'w' object's boolean value and pull it. Similar to how the keyword 'this' works.

I'm new to C++

liberforce
  • 11,189
  • 37
  • 48
  • 2
    Casting `this` to a different type looks plenty suspicious. Perhaps that should be a virtual function instead? (If you're new, pick one from the [book list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).) – molbdnilo Feb 21 '18 at 20:30
  • Do you mean to do `MyClass::get_boolean_val()`? – François Andrieux Feb 21 '18 at 20:31
  • I meant to do `MyClass::get_boolean_val()`. I was told explicitly to have the actual variable protected but still need a way to look at it for conditional statements. – robert strickland Feb 21 '18 at 20:38
  • In addition to heeding @molbdnilo's advice, perhaps also see http://people.scs.carleton.ca/~dehne/projects/cpp-doc/tutorial/tut5-4.html – BadZen Feb 21 '18 at 20:45
  • Well in the first case you are binding `this` and in the second case you are binding `w`. *Are they the same?* – user253751 Feb 21 '18 at 22:41
  • I think they should be the same but when I output the memory addresses they are different. `w` calls the function and inside the function I'm using `this` so I don't know why they are different – robert strickland Feb 22 '18 at 13:16

0 Answers0