error at cast of void ptr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// compiles with -lfltk

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Widget.H>

#include <cstdlib>

void cb_click( Fl_Widget * widget, void * arg )
{
    // sets the Button text
    widget->label( reinterpret_cast<const char *>( arg ) );
    
    widget->deactivate();
}
void cb_exit( Fl_Widget * )
{
    std::exit( EXIT_SUCCESS );
}

const char * message = "Hello there.";

int main()
{
    Fl_Window * window = new Fl_Window( 0, 0, 100, 100, "Test" );

    // make a Button
    Fl_Button * button_click = new Fl_Button( 0, 0, 100, 50, "Click me!" );
    button_click->callback( cb_click, reinterpret_cast<void*>( message ) );
    window->add( button_click );

    Fl_Button * button_exit =  new Fl_Button( 0, 50, 100, 50, "Exit" );
    button_exit->callback( cb_exit );
    window->add( button_exit );

    // ends window creating
    window->end();
    
    // show the window
    window->show();

    Fl::run();
}


At line 30 I get this error message:
[text]
clickme.cpp:30:71: error: reinterpret_cast from type ‘const char*’ to type ‘void*’ casts away qualifiers
30 | button_click->callback( cb_click, reinterpret_cast<void *>(message) );
[/text]
So, what's wrong with and how could I fix it without using plain C-casts?
Try first casting using const_cast to remove the const from const char* then reinterpret_cast to void*. Something like (not tried):

 
button_click->callback( cb_click, reinterpret_cast<void*>(const_cast<char*>( message )));

Thanks, that works.
Topic archived. No new replies allowed.