I'm fairly new to smart pointer usage, and want to make sure I'm not introducing bad habits. I have an object A that is being created from a factory, and added to a queue. This queue is then read from another thread, and at this point I think it's perfectly valid to use a unique_ptr.
struct IInterface
{
}
class A : IInterface
{
}
std::queue<std::unique_ptr<A>> queue;
However, while constructing A I realize I need a Class C. C takes a List of B's that it owns.
class A : IInterface
{
C c;
}
class B
{
}
class C
{
List<unqiue_ptr<B>> b;
}
To me this still looks okay because we have ownership defined. However, I'm unsure of what pointer needs to be used when Class B now needs a pointer to IInterface, which A happens to implement. Do I make it a shared_ptr?
struct IInterface
{
}
class A : IInterface
{
C c;
}
std::queue<std::shared_ptr<A>> queue;
class B
{
}
class C
{
List<shared_ptr<B>> b;
}
Did I choose the correct smart pointer, and did I analyze this correctly? I know this is super simple for you guys, but figured I would ask before I start doing things incorrectly.
Aucun commentaire:
Enregistrer un commentaire