mardi 4 août 2015

Cannot seem to assign a unique_ptr to a struct

I have a unique_ptr member on a class that points to a struct.

class ExampleClass {
    std::unique_ptr<StateStruct> _character_state;
}

I don't understand how to acquire the memory for the struct and set the unique_ptr.

In my constructor I have:

ExampleClass::ExampleClass {
    std::unique_ptr<StateStruct> _character_state(static_cast<StateStruct*>(malloc(sizeof(StateStruct))));
    _character_state->state_member_int_value = 4 // _character_state is empty
}

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire