Attribute vector emptying itself

Posted by ravloony on Stack Overflow See other posts from Stack Overflow or by ravloony
Published on 2010-05-31T20:26:41Z Indexed on 2010/05/31 20:33 UTC
Read the original article Hit count: 163

Filed under:

Hello,

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.

© Stack Overflow or respective owner

Related posts about c++