H
Hermann Lichte
Hi,
I've naively written a piece of code that's not doing what I want it to.
I want to store objects in a vector of some base class A. This base
class provides a method doSomething() that is overridden in subclasses.
When I invoke doSomething() I always end up with the implementation
provided by the base class A, regardless the subclass I used for
instantiating the object. I suppose that this is related to the fact
that the vector holds references and not pointers, but that's what it
has to offer. How can I make it work? I've got some example code here
to illustrate my problem.
#include <iostream>
#include <vector>
using namespace std;
class A {
public:
A();
virtual ~A();
virtual double doSomething(int parameter);
};
A::A()
{
}
A::~A()
{
}
double A::doSomething(int parameter)
{
cout << "A: " << parameter << "\n";
return 0.0;
}
class B : public A {
public:
B();
virtual ~B();
virtual double doSomething(int parameter);
};
B::B()
{
}
B::~B()
{
}
double B::doSomething(int parameter)
{
cout << "B: " << parameter << "\n";
return 0.0;
}
class C
{
vector<A> Items;
public:
C();
~C();
void addItem(A& item);
A* getItem(int index);
};
C::C() : Items()
{
}
C::~C()
{
}
void C::addItem(A& item)
{
Items.push_back(item);
}
A* C::getItem(int index)
{
return &Items.at(index);
}
int main()
{
C storage;
B* object = new B();
storage.addItem(*object);
A* another = storage.getItem(0);
A* another2 = object;
another->doSomething(123); // doesn't call B::doSomething()
// but that's what I want
another2->doSomething(123); // does call B::doSomething()
delete object;
return 0;
}
Any advice is appreciated.
Kind regards,
Hermann
I've naively written a piece of code that's not doing what I want it to.
I want to store objects in a vector of some base class A. This base
class provides a method doSomething() that is overridden in subclasses.
When I invoke doSomething() I always end up with the implementation
provided by the base class A, regardless the subclass I used for
instantiating the object. I suppose that this is related to the fact
that the vector holds references and not pointers, but that's what it
has to offer. How can I make it work? I've got some example code here
to illustrate my problem.
#include <iostream>
#include <vector>
using namespace std;
class A {
public:
A();
virtual ~A();
virtual double doSomething(int parameter);
};
A::A()
{
}
A::~A()
{
}
double A::doSomething(int parameter)
{
cout << "A: " << parameter << "\n";
return 0.0;
}
class B : public A {
public:
B();
virtual ~B();
virtual double doSomething(int parameter);
};
B::B()
{
}
B::~B()
{
}
double B::doSomething(int parameter)
{
cout << "B: " << parameter << "\n";
return 0.0;
}
class C
{
vector<A> Items;
public:
C();
~C();
void addItem(A& item);
A* getItem(int index);
};
C::C() : Items()
{
}
C::~C()
{
}
void C::addItem(A& item)
{
Items.push_back(item);
}
A* C::getItem(int index)
{
return &Items.at(index);
}
int main()
{
C storage;
B* object = new B();
storage.addItem(*object);
A* another = storage.getItem(0);
A* another2 = object;
another->doSomething(123); // doesn't call B::doSomething()
// but that's what I want
another2->doSomething(123); // does call B::doSomething()
delete object;
return 0;
}
Any advice is appreciated.
Kind regards,
Hermann