I often encounter situations where I need to store lists of non-owning pointers or references to base class objects. Of course, I could do
#include <initializer_list>
#include <list>
class Base {};
class Derived {};
class Container {
public:
void setObjects(const std::initializer_list<const Base *> objects); // Set objects_ member
private:
std::list<const Base *> objects_; // Should store Base and Derived objects
};
Using std::reference_wrapper
, I could also use
#include <initializer_list>
#include <list>
#include <functional> // for std::reference_wrapper
class Base {};
class Derived {};
class Container {
public:
void setObjects(const std::initializer_list<std::reference_wrapper<const Base &> > objects); // Set objects_ member
private:
std::list<std::reference_wrapper<const Base &> > objects_; // Should store Base and Derived objects
};
When I want to express the fact that an object (an instance of the Container
class in my case) cannot exist without other objects (instances of the Base
or Derived
class), I tend to prefer the second alternative. However, it feels quite verbose to me and I have rarely seen it in other code. Is there any good reason I should prefer one alternative over the other?
Aucun commentaire:
Enregistrer un commentaire