Re: 'const' member functions
Just a guess (never have used GTK): Have you tried this?
Code:
const Glib::RefPtr<const Gdk::Window> p = canvas_event_box.get_window();
Re: 'const' member functions
Addendum...
I discovered that I can obtain pointer 'p' by moving the position of const - i.e. this doesn't compile...
Code:
void functionB() const
{
const Glib::RefPtr<Gdk::Window> p = canvas_event_box.get_window();
}
but this does compile...
Code:
void functionB() const
{
Glib::RefPtr<const Gdk::Window> p = canvas_event_box.get_window();
}
The problem now is that having succesfully obtained the pointer, I can't use it !!!
My ultimate aim is to find the 'x' and 'y' co-ordinates for this window's mouse pointer:-
Code:
void functionB() const
{
gint x, y;
Gdk::ModifierType state;
Glib::RefPtr<const Gdk::Window> p = canvas_event_box.get_window();
p->get_pointer (x, y, state);
}
But the red line now gives me this error :cry:
Quote:
error C2662: 'Gdk::Window::get_pointer' : cannot convert 'this' pointer from 'const Gdk::Window' to 'Gdk::Window &'
1> Conversion loses qualifiers
:cry: :cry: :cry:
Re: 'const' member functions
Which line is line 199?
Quote:
I don't actually want to change anything in the member variable canvas_event_box
Don't use functions that imply that you do want to change the value. If a function is non-const, and you decide to use that function on a non-mutable variable, then nothing is going to fix that except to redesign your code.
Regards,
Paul McKenzie
Re: 'const' member functions
Quote:
Originally Posted by
John E
The problem now is that having succesfully obtained the pointer, I can't use it !!!
My ultimate aim is to find the 'x' and 'y' co-ordinates for this window's mouse pointer:-
Code:
void functionB() const
{
gint x, y;
Gdk::ModifierType state;
Glib::RefPtr<const Gdk::Window> p = canvas_event_box.get_window();
p->get_pointer (x, y, state);
}
But the red line now gives me this error
If get_pointer() is non-const, then again, you're trying to call a non-const member function on a const object. That isn't going to work.
You need to go through the public interface of whatever Gdk::Window is, and identify which functions are const, which are non-const, and use the appropriate function.
Regards,
Paul McKenzie
Re: 'const' member functions
Gdk::Window::get_pointer() is declared like this:-
Code:
Glib::RefPtr<Window> get_pointer(int& x, int& y, ModifierType& mask);
Fortunately I'm building from source so I can easily add an overrided version like so:-
Code:
Glib::RefPtr<Window> get_pointer(int& x, int& y, ModifierType& mask) const;
Before I do that though... isn't there any way that const_cast could come to my aid here?
Re: 'const' member functions
Quote:
Originally Posted by
John E
Before I do that though... isn't there any way that const_cast could come to my aid here?
I might have answered my own question... this seems to do the trick (at least it compiles. anyway)
Code:
void Editor::functionB() const
{
gint x, y;
Gdk::ModifierType state;
Glib::RefPtr<Gdk::Window> p = const_cast<Editor*>(this)->track_canvas_event_box.get_window();
p->get_pointer (x, y, state);
}
Re: 'const' member functions
Quote:
Originally Posted by
John E
I might have answered my own question... this seems to do the trick (at least it compiles. anyway)
What does the implementation of get_pointer() look like? If it mutates the member variable in any way, then issuing a const_cast is dangerous if you're really calling a non-const function.
Why would the authors of get_pointer() make it non-const? Is it because they were lazy, didn't do their due diligence in making the function const, or for legitimate reasons?
You need to answer that question before using const_cast.
Regards,
Paul McKenzie
Re: 'const' member functions
Good point. I wouldn't have thought of that....
It looks reasonably safe (I think). It just wraps a standard 'C' function (which returns a GdkWindow*). That's then used to create a Glib::RefPtr which gets returned to the caller.
Code:
Glib::RefPtr<Window> Window::get_pointer(int& x, int& y, ModifierType& mask)
{
Glib::RefPtr<Window> retvalue = Glib::wrap((GdkWindowObject*)(gdk_window_get_pointer(gobj(), &x, &y, ((GdkModifierType*) &(mask)))));
if(retvalue)
retvalue->reference(); //The function does not do a ref for us.
return retvalue;
}
BTW 'Window' is in the namespace Gdk - so the type of pointer is Glib::RefPtr<Gdk::Window>
Re: 'const' member functions
Quote:
Originally Posted by
John E
Good point. I wouldn't have thought of that....
It looks reasonably safe (I think). It just wraps a standard 'C' function (which returns a GdkWindow*). That's then used to create a Glib::RefPtr which gets returned to the caller.
You can create a duplication function, stick the const keyword at the end of it, call it in your main function, and compile.
If the compiler doesn't like it, you'll know what in there can be potentially dangerous.
Regards,
Paul McKenzie
Re: 'const' member functions
Quote:
Originally Posted by
Paul McKenzie
You can create a duplication function, stick the const keyword at the end of it, call it in your main function, and compile.
If the compiler doesn't like it, you'll know what in there can be potentially dangerous.
Thanks. I tried that a little earlier and it compiled OK. However, it would give me a non-standard API - which isn't a massive problem and I'd probably do it that way if there was any danger but for the moment, I think I'd prefer to stick with the const_cast fix. Thanks for all your help Paul.
Re: 'const' member functions
Quote:
Originally Posted by
John E
Thanks. I tried that a little earlier and it compiled OK. However, it would give me a non-standard API
Is it the one where you're passing a temporary object to a function that expects the parameter to be non-const?
If so, that actually is an error in C++ proper -- it's just that Visual C++ considers it only a warning (assuming you set the warning level to 4)
Regards,
Paul McKenzie