-2

I've got a function Quit that is receiving a void** and I need to pass it to a new function who's receiving a DataStructure*.

The problem is that the data in ds is replaced with garbage. What can I do ?

void Quit(void** DS){
       DataStructure* ds = (DataStructure*) *DS;
       return ds->Quit(); //'void*' is not a pointer to object type
}

when the definition of the second Quit() is:

void DataStructure::Quit();

and the call in main is done as follow:

DataStructure *data;
Quit((void**) &ds);

We get the same result if we cast as follow:

 DataStructure* ds = reinterpret_cast<DataStructure*>(*DS);      
R.Gad
  • 11
  • 2

2 Answers2

2

The cast works, it just doesn't change the type of DS like you expect it to do.

((DataStructure*)* DS) converts the value of *DS and throws the result away.
(Your compiler might have warned you that it has no effect.)

You need to save the result of the conversion:

void Quit(void** DS){
       DataStructure* ds = (DataStructure*) *DS;
       return ds->Quit();
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • 1
    or better yer, `DataStructure* ds = reinterpret_cast(*DS);` – YSC Dec 07 '16 at 12:43
  • It pass the compilation but I lose what was in DS (for every answer). I don't want to lose that data. Indeed, DS is of a DataStructure** type but my function get only void**. – R.Gad Dec 07 '16 at 13:48
  • @R.Gad Post the code that creates this `void**` as well. – molbdnilo Dec 07 '16 at 13:52
1

If your DS variable actually is a DataStructure* you can simply cast it:

void Quit(void** DS)
{
       DataStructure* ds = (DataStructure*)DS;
       return ds->Quit();
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142