CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2015
    Posts
    1

    Store subclasses of objects in a vector

    Hello. PLease can I get some help with this show stopper? Refering to my original problem on Stack Overflow I'm trying to store a collection of subclasses into a vector. However I can't seem to retrieve the subclass members due to object slicing.

    How can I iterate over vector calling each object as it's original subclass?

    Code:
    class Object{
    public:
        ~Object();
        std::string name;
    };
    
    class Rect : public Object
    {
    public:
        int x;
        int y;
    };
    
    int main()
    {
        std::vector<Object*> objects;
    
        Rect* r = new Rect();
        r->name = "test";
        r->x = 1;
        r->y = 1;
    
        objects.push_back(r);
    
        for(auto& ob: objects)
        {
            std::cout << ob->x << std::endl;
        }
    
        return 0;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Store subclasses of objects in a vector

    Quote Originally Posted by fatnic View Post
    Hello. PLease can I get some help with this show stopper? Refering to my original problem on Stack Overflow I'm trying to store a collection of subclasses into a vector. However I can't seem to retrieve the subclass members due to object slicing.

    How can I iterate over vector calling each object as it's original subclass?
    But the answer is in the original problem thread on Stack Overflow you referred to!
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Store subclasses of objects in a vector

    Quote Originally Posted by fatnic
    How can I iterate over vector calling each object as it's original subclass?
    Why do you want to do this? If Object were a polymorphic base class, you could use dynamic_cast to achieve this, but it would be better if you declared a virtual function in the base class that each subclass can override to do precisely what you want from each subclass object.

    Speaking of a polymorphic base class, Object is not one at the moment. You did not declare Object as having a virtual destructor, so delete via objects would result in undefined behaviour... then again you forgot to delete. Consider using say, a std::vector<std::unique_ptr<Object>> instead, as was suggested to you on Stack Overflow.

    EDIT:
    Quote Originally Posted by VictorN
    But the answer is in the original problem on Stack Overflow you referred to!
    No, fatnic modified the code according to the answer on Stack Overflow (notice objects is a std::vector<Object*> here whereas it was a std::vector<Object> on Stack Overflow), then for some reason decided to pursue the next step here instead of over there.
    Last edited by laserlight; August 18th, 2015 at 12:27 PM.
    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

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Store subclasses of objects in a vector

    what you ask is not possible if you insist on a std::vector<Object>

    the vector only has room for Objects, and subclasses are "bigger" so they don't fit. When you add subclass to this vector, it'll only keep the Object part of it. THis is how C++ works.

    if you have a std::vector<Object*> however, then yes, it's possible, but you will also need something else to know that your Object* in that vector is really something bigger.
    One way would be to only use functions and virtual functions of Object.
    Or use dynamic downcasting using the compiler's RTTI system
    Or use dynamic downcasting using your own form of type information
    Either way, none of those solutions is typically going to ideal. downcasting is something you want to avoid, so the real problem is. Why do you want to do things this way ? There's probably another design approach that will do what you want to achieve in a much nicer way, though it may require elaborate changes to your code to adopt the new design.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured