Storing objects of an hierarchy in a vector
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Storing objects of an hierarchy in a vector

  1. #1
    Join Date
    Dec 2001
    Posts
    31

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,261

    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>.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Dec 2001
    Posts
    31

    Re: Storing objects of an hierarchy in a vector

    Thank you very much for your help and time.

    Quote Originally Posted by laserlight View Post
    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 View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,261

    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>.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Dec 2001
    Posts
    31

    Re: Storing objects of an hierarchy in a vector

    Quote Originally Posted by laserlight View Post
    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,261

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,290

    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 LITE 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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center