I have a class setup analogous to this:
class BlimpBase{
public:
virtual ~BlimpBase();
private:
virtual void lift()const = 0;
};
class Blimp: protected BlimpBase{
void lift()const;
};
class BlimpCarrier{
public:
add_blimp(BlimpBase& blimp);
private:
std::vector<BlimpBase* blimp> blimps;
};
As you can see, I have a set of polymorphic blimp classes and I am trying to store then as "references" in the vector by using pointers (I realize you can't store references in vectors, I just don't know how else to describe them). The problem is that most of these objects are allocated on the stack as class members, but I want to add them to this vector so that I can directly modify them (copies won't do). The problem with keeping pointers to these objects is that if these objects go out of scope before the BlimpCarrier
does (since they are on the stack), I will have a dangling pointer. I looked into std::unique_ptr
and std::shared_ptr
, but I don't think I can use them here...