lundi 29 janvier 2018

Initialization of const pointer to another member of the same struct

I'm creating a struct in which one member is a const pointer (immutable address) to another member of the struct.

In the simplified version below, will both structs always behave the same way? Especially in the sense of whether the addresses stored in ptr are guaranteed to be properly initialized.

struct First
{
  int a;
  int* const ptr = &a;
};

struct Second
{
  int a;
  int* const ptr;

  Second() : ptr(&a) {}
};

(In my actual application the member a is a class instance, and ptr is replaced by a map from some enums to pointers pointing to members of a.)

Aucun commentaire:

Enregistrer un commentaire