vector of pointers

Nov 21, 2012 at 5:23pm
I was wondering if there is a difference between:

1
2
3
vector<Course*> courses;
Course* cs = new CScourse(); //CScourse inherits Course
courses.push_back(cs);


and

1
2
3
void something(Course& c){ //c is also located on heap
courses.push_back(&c);
}


is there a difference between pushing a pointer and pushing reference?
Nov 21, 2012 at 5:38pm
is there a difference between pushing a pointer and pushing reference?

There is, although in both your examples above you're pushing a pointer to a Course object.
Nov 21, 2012 at 5:39pm
¿can you `store' references?
Nov 21, 2012 at 5:40pm
You are not pushing a reference. In the both cases you are pushing a pointer to an object of type Course I think that using CScourse() in the first example is a typo) . The difference is that in the first case we know that we are pushing a pointer that points to an object created in the heap while in the second case we can say nothing about where the object was created.
Last edited on Nov 21, 2012 at 5:41pm
Nov 21, 2012 at 5:43pm
The same could be say with
1
2
3
void foo(Course *bar){
   courses.push_back(bar);
}
Nov 21, 2012 at 7:21pm
got it! Thnx!
Topic archived. No new replies allowed.