Storing objects of an hierarchy in a vector
Hello,
I have a hierarchy of Actions (NonMovingActions and MovinActions each with sub-hierarchies). Actions class has an abstract function.
Code:
class Action
{
public:
Action(Agent& agent, ...):agent(agent),..{}
virtual ~Action(void);
virtual bool run()=0;
...
}
Now I need to store all actions into a single vector:
Code:
class Agent
{
public:
Agent(World& world);
virtual ~Agent(void);
...
vector<Action> actions;
unsigned int actionIterator;
}
It appears that C++ does not allow this (in Java it was possible). Compiler objects that Action class is abstract (and cannot be instantiated?!)
Code:
error C2259: 'Action' : cannot instantiate abstract class
1- May I know what do I not understand here? We cannot refer to sub-class instances with a reference of parent class type?
2- Should I use vector of pointers instead or what?
I appreciate your time and help.
Regards,
Mac
Re: Storing objects of an hierarchy in a vector
Quote:
Originally Posted by sarmadys
It appears that C++ does not allow this (in Java it was possible).
It is not possible in Java either since Java only has reference variables for objects.
Quote:
Originally Posted by sarmadys
1- May I know what do I not understand here? We cannot refer to sub-class instances with a reference of parent class type?
You can. However, Action is not a reference (or pointer). A vector<Action> is a vector of objects where each object is of the Action class, and not of any Action subclass. Since Action is an abstract base class, you thus cannot have a vector<Action> since objects of the Action class itself cannot exist.
Quote:
Originally Posted by sarmadys
2- Should I use vector of pointers instead or what?
Yes, or a vector of smart pointers, or a boost::ptr_vector<Action>.
Re: Storing objects of an hierarchy in a vector
Thank you very much for your help and time.
Quote:
Originally Posted by
laserlight
It is not possible in Java either since Java only has reference variables for objects.
I used to have an ArrayList<Action> and just add all sub-class instances into it and it would not object.
Quote:
Originally Posted by
laserlight
Yes, or a vector of smart pointers, or a boost::ptr_vector<Action>.
So by using pointers now I should use "new" and manage the dellocation myself using "delete". right? It is going to be more messy. That's why I try to use C++ automatic "garbage collection LIKE" mechanism.
Thank you again.
Re: Storing objects of an hierarchy in a vector
Quote:
Originally Posted by sarmadys
I used to have an ArrayList<Action> and just add all sub-class instances into it and it would not object.
You are mistaken: you did not "add all sub-class instances into it". You added references to those instances to the ArrayList. In colloquial terminology we say "instance" in Java to also mean a reference to that instance, but technically you are storing references to the instances, not the instances themselves.
Quote:
Originally Posted by sarmadys
So by using pointers now I should use "new" and manage the dellocation myself using "delete". right?
Yes, unless you are just storing pointers that point to existing objects.
Quote:
Originally Posted by sarmadys
It is going to be more messy. That's why I try to use C++ automatic "garbage collection LIKE" mechanism.
Use vector of smart pointers or a boost::ptr_vector<Action>.
Re: Storing objects of an hierarchy in a vector
Quote:
Originally Posted by
laserlight
Use vector of smart pointers or a boost::ptr_vector<Action>.
I am sorry to take more time of you. Boost documentation for ptr_vector does not describe what is exactly the benefit of using it instead of std::vector. May I know that?
Re: Storing objects of an hierarchy in a vector
Quote:
Originally Posted by sarmadys
Boost documentation for ptr_vector does not describe what is exactly the benefit of using it instead of std::vector. May I know that?
It is stated in the general documentation for Boost pointer containers.
Re: Storing objects of an hierarchy in a vector
Alternatively, You could create an ActionHolder class, which stores a pointer to an Action.
Also known as the opaque pointer design pattern.