• Problem with vectors and iterators
    9 replies, posted
It only outputs "Animal" twice instead of Animal and Dog. [code] #include <iostream> #include <vector> using namespace std; //Animal base Class class Animal { public: virtual void Talk() { cout << "Animal\n"; } }; //Dog class class Dog : public Animal { public: void Talk() { cout << "Dog\n"; } }; int main() { vector<Animal> animals; vector<Animal>::iterator Iter; Animal animal; Dog dog; animals.push_back(animal); animals.push_back(dog); for (Iter = animals.begin(); Iter != animals.end(); ++Iter ) { (*Iter).Talk(); } return 1; } [/code]
You need to mark Dog::Talk as virtual as well. Currently it's just hiding the base class' Talk method.
Thanks, but it stills output Animal twice. [code] class Animal { public: virtual void Talk() { cout << "Animal\n"; } }; class Dog : public Animal { public: virtual void Talk() { cout << "Dog\n"; } }; [/code]
Your vector needs to hold a pointer to the Animal class type for this to work.
I don't think that is the issue, since a vtable is created and in both cases it should access the vtable and not do a direct function call. It's rather that your Dog will not be pushed into the vector, since push_pack copies entries. So, what happens is that on your second push_back Dog will implicitly be casted to Animal, then the copy-constructor of Animal gets called and in the end you have two Animal-objects in your vector. Ninja'd. Though I have a proper explanation :v:
Now, it works, thanks :). [code] #include <iostream> #include <vector> using namespace std; class Animal { public: virtual void Talk() { cout << "Animal\n"; } }; class Dog : public Animal { public: virtual void Talk() { cout << "Dog\n"; } }; vector<Animal*> animals; vector<Animal*>::iterator Iter; int main() { Animal animal; Dog dog; animals.push_back(&animal); animals.push_back(&dog); for (Iter = animals.begin(); Iter != animals.end(); ++Iter ) { (*Iter)->Talk(); } return 1; } [/code] But if i want a function to add objects to the vector it wont.For example [code] void AddDog() { Dog dog; animals.push_back(&dog); } [/code] Is there a way to store a copy in the vector instead of the pointer?
[cpp]void AddDog() { Dog dog; //object dog gets created animals.push_back(&dog); //address of object dog gets copied in vector } //object dog gets destructed D:[/cpp] You are creating an invalid pointer there. You can create the object on the heap via the operator new, but make sure to deallocate all that memory with operator delete when you don't need it anymore (e.g. when the program is about to exit).
Thanks you. Now it works perfectly. [CODE] for (Iter = animals.begin(); Iter != animals.end(); ++Iter ) { (*Iter)->Talk(); delete *Iter; } [/CODE] [CODE] void AddDog() { animals.push_back(new Dog()); } [/CODE]
You should avoid delete objects inside an Iterator loop. If you just want to empty the vector you can do this: [code] while (vector.empty() == false) { delete vector.back(); vector.pop_back(); }[/code] This was written in the reply box so it's probably not going to work.
Ok, it worked. Thanks for the tip.
Sorry, you need to Log In to post a reply to this thread.