-
June 6th, 2012, 02:52 AM
#1
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
-
June 6th, 2012, 03:04 AM
#2
Re: Storing objects of an hierarchy in a vector
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.
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.
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>.
-
June 6th, 2012, 03:27 AM
#3
Re: Storing objects of an hierarchy in a vector
Thank you very much for your help and time.
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.
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.
-
June 6th, 2012, 03:37 AM
#4
Re: Storing objects of an hierarchy in a vector
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.
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.
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>.
-
June 6th, 2012, 03:45 AM
#5
Re: Storing objects of an hierarchy in a vector
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?
-
June 6th, 2012, 03:48 AM
#6
Re: Storing objects of an hierarchy in a vector
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.
-
June 6th, 2012, 04:21 AM
#7
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.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|