Closed LoshkinOleg closed 4 years ago
Il faut remplacer:
public:
FixedMap(FreeListAllocator& allocator)
{
allocator_ = allocator;
pairs_.resize(Size); // Initializes the pairs to nullptr.
}
Par:
public:
FixedMap(FreeListAllocator& allocator) : allocator_(allocator)
{
pairs_.resize(Size); // Initializes the pairs to nullptr.
}
Ah ouaip, en effet. Pourquoi au fait il saisi pas qu'on initialise la variable si c'est dans le body de la fonction au fait?
C'est parce que le fait de setter la valeur dans le corps du constructeur n'est pas explicite.
Ouaip juste. Au fait avec la 1ere methode on peux faire:
FixedMap(FreeListAllocator& allocator)
{
allocator_.someField = 3; // Accessing nullptr!
allocator_ = allocator;
}
Alors qu'avec la 2ème methode ya pas moyen de glisser du code entre l'appel du constructeur et l'initialisation de allocator_ .
J'ai une erreur de compilation qu'il me sort lorsque j'essaye de passer une reference d'allocator au constructeur d'une FixedMap(FreeListAllocator&):
Code du TEST:
Code du constructeur:
Erreur:
Je saisi pas pourquoi il voit pas que je suis en train de setter le field... Est-ce que j'utilise l'allocator d'une mauvaise manière?