I have two classes, derived from a common class.
The common class has a pure virtual function called execute(), which is implemented in both derived classes.
In the inherited class I have an attribute which is a vector. In both execute() methods I overwrite this vector with a result. I access both classes from a vector of pointers to their objects.
The problem is when I try to access the result vector form outside the objects. In one case I can get the elements (which are simply pointers), in the other I cannot, the vector is empty.
Code:
class E;
class A{
protected:
vector<E*> _result;
public:
virtual void execute()=0;
vector<E*> get_result();
};
vector<E*> A::get_result()
{
return _result;
}
class B : public A
{
public:
virtual void execute();
};
B::execute()
{
//...
_result = tempVec;
return;
}
class C : public A
{
public:
virtual void execute();
};
C::execute()
{
//different stuff to B
_result = tempvec;
return;
}
main()
{
B* b = new B();
C* c = new C();
b->execute();
c->execute();
b->get_result();//returns full vector
c->get_result(); //returns empty vector!!
}
I have no idea what is going on here… I have tried filling _result by hand from a temp vector in the offending class, doing the same with vector::assign(), nothing works. And the other object works perfectly. I must be missing something….
Any help would be greatly appreciated.
Since everything else is the same between classes
BandCI would have to say that the lineis probably important!
Also your
get_result()method should really beto save you making a copy of the vector each time.