a (private) vector of my own objects + some additional variables, how do i make the object instances of that class have the same methods as a vector has? for example:
MyCont cont = {my objects here}
and i want to do things like:
cont[2]
cont.push_back()
cont.end()
etc.
i see 2 solutions: return the vector and do stuff on it:
// in the class:
std::vector<Obj> vector(){
return m_vector;
}
// in the main file:
cont.vector()[2]
cont.vector().push_back()
or, possibly, somehow adding all of the methods of the standard vector class and using them as a sort of an interface:
//in the class:
void push_back(Obj& obj){
m_vector.push_back(obj);
}
// and the same for each method
but that seems very error-prone and just overall unstable, and i wonder if there's a more elegant workaround to what i'm looking for / how the same problem is solved in more professional apps
Hey, take a look at this stackoverflow Question, which adds another option to your list, and also discusses the problems of that option. Hope you will find it useful: https://stackoverflow.com/questions/4353203/thou-shalt-not-inherit-from-stdvector
Обсуждают сегодня