I am working on a project as homework for my university system programming course. I am very confused about the issue of pointers, vectors, stacks and heaps.
Using C ++. I have to get a vector of objects that are courses, and those course objects have several different fields. I have done this:
vector<CoursesObject> coursevector;
and then I created a class in the Courses class that contains the space left in the course and the name of the course fields. Now I want to add a new course:
CoursesObject *theCourse = new CoursesObject(name, space);
Now I want to add it to the handler vector:
coursevector.push_back(*theCourse);
With all that I know, I created a vector of Courses objects on the stack and made a new pointer to a new course that is on the heap, and added theCourse pointer to the handler vector, which points to the course object in the heap. Is this what I said correctly?
When I try to delete those course objects, I do:
for(int i=0; i<coursevector.size(); i++) delete coursevector.at(i);
which gives me an error that this is not a pointer. But haven't I added a POINTER to the course object in the cursor stream?
Please explain, I have to handle memory correctly, and it seems that I do not understand.
source share