I am not very experienced with C ++ yet, so bear with me if this is the main material.
I have code like below. Lis an abstract class (it has a number of pure virtual functions), and A, Band Care regular classes, all derived from L. There can be any number of them, and they are all different.
int main() {
std::vector<L*> ls(3) ;
ls[0] = new A ;
ls[1] = new B ;
ls[2] = new C ;
int i ;
for (i = 0 ; i < ls.size() ; i++) {
if (ls[i]->h()) {
}
}
}
This works, but there really should be a better way to initialize this vector. Correctly?
The vector should not change after it has been initialized first. I believe that I cannot do this const, because different objects can themselves change inside. I chose a vector over a regular array, because I do not want to manually track its length (which turned out to be error prone).
main , #include. , , " , " = ". A, B C .
, , delete - new, ls delete delete[]. delete ls;, , "type" class std::vector < L *, std:: allocator < L * → , ", ".
?
user606547