Suppose I’m writing a C++ staff management system with STL
I have an entity class called StaffMember:
class StaffMember
{
//
}
and a repository called Staff:
class Staff
{
private:
std::vector<StaffMember> staffMembers;
}
How do I know if I should declare staffMembers as a std::vector<StaffMember> or std::vector<StaffMember>*?
As a pointer, I would need to delete it inside ~Staff(), otherwise I would not.
Both will work, but the question is, which one do I choose and is there a general rule I can use when this question pops up in the future?
Typically by value (
std::vector<StaffMember>), unless you need to share it. Even if you do need to share it, a smart pointer is much better than a raw pointer.Compilation firewalls are an exception (e.g. PIMPL).