Когда сообщение подготовлено, я ожидаю создать новую структуру state_t, ссылающуюся на новый yданные. Затем я хочу иметь возможность доступа к новому объекту через ту же переменную состояния.
Минимальный пример с указателями будет примерно таким:
Код: Выделить всё
class Handler {
int x; // something config for the handler
struct state_t {
int *y;
state_t(int *y) : y(y) {}
};
std::optional state;
Handler(int x) : x(x), state(std::nullopt) {}
void prepare_message(int *y) {
state = state_t(y);
}
void handle_state() {
// do something with the y e.g. send it over a socket
}
};
Подробнее здесь: https://stackoverflow.com/questions/791 ... -reference