Ok, let me try once again, what I am trying to say is:
T *aptr;
and then using
aptr->pop_back();
is not valid.
If you would have had something like
vector<T> aobj;
and then if you would have done
aobj.pop_back();
OR
vector<T> *aptr;
and then
aptr->pop_back();
then these two are valid.
Because
vector<T> aobj;
and
vector<T> *aptr;
ARE VECTOR TYPES. So we can use it's provided functions like
pop_back();
. However, if you write
T *aptr;
and then you write
SimpleVector<int> obj;
then since your
SimpleVector
class is a template class, that means where ever you have written
T
, it would be replaced by the data type you have specified in
SimpleVector<int> obj;
in this case this is an
int
Try this simple code
1 2 3 4 5 6 7
|
int main() {
int i = 10;
int *aptr = &i;
aptr->pop_back(); //invalid
return 0;
}
|
Do you think above code is valid? NO, it is not valid because
int
is not a class, hence there is no such member function defined named as
pop_back();
. On the other hand
vector
is a Container Class. Therefore, it has a member function named
pop_back();
defined in it's definition. Therefore, you can use
pop_back();
only on the vector type of objects and pointers.