I have two classes. One class(Person) has a vector collection composed of pointers of the other class(Student). At run time, the Person class will call a method which will store a pointer to a Student class in the vector. I have been trying to do this with smart pointers to avoid Memory leak issues that can arise but I am struggling to do so. How would I go about it?
My goal is for the Person class to have handles to objects that exist somewhere else in the code
Class Student
{
public:
string studentName
Student(string name){
studentName = name;
}
}
Class Person
{
public:
vector <Student*> collection;
getStudent()
{
cout << "input student name";
collection.push_back(new Student(name));
}
}
You don't need to use smart pointers here. Place the objects directly into the vector and their lifetime is managed by the vector:
std::vector<Student> collection;
collection.emplace_back(name);