Hello Everyone.
So I have a base class, lets call it base.
In base I have a virtual function called update(), update just couts "base"
then I have a class derived from base called derived;
it has a function called update(), update just couts "derived"
then I create a vector called Vec it's initialised like this:
then I add an element into it like this
1 2
|
Derived DerElement;
Vec.push_back(DerElement);
|
then when I type:
1 2 3 4
|
for (int i=0; i<Vec.size(); i++)
{
Vec.at(i).Update();
}
|
it outputs:
and I know it works normally (without using vectors) cause then I tried this
1 2
|
Derived DerElement2;
DerElement2.Update();
|
and it outputs this:
here is a full example of code that will generate this problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
#include <vector>
class Base
{
public:
virtual void Update()
{
std::cout << "Base" << std::endl;
}
};
struct Derived : public Base
{
public:
void Update()
{
std::cout << "Derived" << std::endl;
}
};
int main()
{
std::vector<Base> Vec;
Derived DerElement;
Vec.push_back(DerElement);
for (int i=0; i<Vec.size(); i++)
{
Vec.at(i).Update();
}
Derived DerElement2;
DerElement2.Update();
}
|
and this is it's output:
Base
Derived
Press any key to continue . . .
|
any help on this would be greatly appreciated, ThankYou.