Casting void * user_data for an object

how do i make an void *somethingobject in standard c ++? In particular, I want to use void *userdata upstd::map<String, void*>

Is it possible? I'm trying to:

//void *user_data is a parameter of this function (callback)
std::map <String, void*> user_data_n; //this line is ok
user_data_n = static_cast<std::map<String, void *>>(*user_data); //I get the errors here.

ERRORS:

Spurious '>>' user '>' to terminate a template argument list
Expected '>' before '(' token
'void *' is not a pointer-to-object type

or is there a better way to transfer information about the object of the caller and some other parameters that I can pass to void *user_data?

UPDATE:

Ass, suggested by @aaa carp, I changed >>to > >, and the first two errors were resolved. The last is strange: why do I get such a message when using it, and not when setting this object when setting up a callback?

std::map<String, void*> user_data_h;
user_data_h["Object"] = this; //this is a MainController object
user_data_h["h"] = h; //h was defined as int *h
createTrackbar("trackbar_H", winName, h, 255, trackbar_handler, &user_data_h);

where createTrackbar is defined as:

int createTrackbar( const string& trackbarname, const string& winname,
int* value, int count, TrackbarCallback onChange, void* userdata);

UPDATE2:

, , , , ?

void trackbar_handler(int value, void *user_data){
std::map <String, void*> *user_data_map;
user_data_map = reinterpret_cast<std::map<String, void *> *>(user_data); //WORKED!! ;)

MainController *controller; //the same class type I put using "this" above
controller = reinterpret_cast<MainController *>( user_data_map["Object"]); //ERROR here

int *var = reinterpret_cast<int*> (user_data_map["h"]); //ERROR also here
+3
4

void *, . , :

std::map <String, void*> *user_data_n;

-, reinterpret_cast ( ) :

user_data_n = reinterpret_cast<std::map<String, void *> *>(user_data);

Update:

, static_cast.

, ?

void *, .

, ?

, . , :

std::map <String, void*> &user_data_map =
    *(static_cast<std::map<String, void *> *>(user_data));
+1

>> > > void, void ,

@casa

+6

, β†’ β†’ ( ).

, user_data void, . reinterpret_cast:

std::map <String, void*> *user_data_n_ptr; //note this is a pointer to a map.
user_data_n_ptr = reinterpret_cast<std::map<String, void *> *>(user_data);

void std:: map.

. ++ void. , , void * .

+1

, C? , struct, , . , . - :

struct callback_user_data {
  my_class* that;
  int number;
  callback_user_data(my_class* p, int i) : that(p), number(i) {}
};

// the callback
void my_callback(void* user_data)
{
  callback_user_data* cbud = static_cast<callback_user_data*>(user_data);
  somehow_use(cbud->that, cbud->number);
}

//call the function, passing our user data
callback_user_data cbud(this, 42);
some_function_taking_our_callback(&my_callback, &cbud);

, ( ) , , this, :

// the callback
void my_callback(void* user_data)
{
  my_class* that = static_cast<my_class*>(user_data);
  that->f();
  std::cout << that->number << '\n';
}

//call the function, passing our user data
some_function_taking_our_callback(&my_callback, this);
+1

Source: https://habr.com/ru/post/1765937/


All Articles