CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2012
    Posts
    19

    Subclass objects in one array?

    Hi

    i want to save similar objects, meaning having same superclass, in an array. Example: I have a superclass vehicle. Class car and truck inherit from vehicle. Now i have a few from both of them an want to put them in an array, vector, set or else. Is this possible in C++ or are there an workarounds? When i remember right, Java and C# offer this feature.


    Here again an example:
    Code:
    class vehicle{
      int mMaxSpeed;
    }
    class truck : public vehicle{
      int mTrailerLength;
    }
    class car : public vehicle{
      bool mHasHitch;
    }
    
    main()
    {
      truck mytruck = new truck();
      car mycar = new car();
    
      vector<vehicle> myCarPool;
      myCarPool.pushback(mytruck);
      myCarPool.pushback(mycar);
    }
    Greets

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Subclass objects in one array?

    You can't store objects of different classes into a vector but you can store a base pointer. To avoid having memory leaks due to forgotten deletes you can make the vector store a smart pointer like boost::shared_ptr http://www.boost.org/doc/libs/1_50_0...shared_ptr.htm or std::shared_ptr if your compiler supports them. http://en.cppreference.com/w/cpp/memory/shared_ptr
    Last edited by S_M_A; July 16th, 2012 at 03:25 AM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

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

    Re: Subclass objects in one array?

    ... Besides, your "syntax" for new and main() looks like to be incorrect. Did you probably mean:
    Code:
    int main()
    {
      truck* mytruck = new truck();
      car* mycar = new car();
    
      vector<vehicle*> myCarPool;
      myCarPool.pushback(mytruck);
      myCarPool.pushback(mycar);
      ...
    
      delete mycar ;
      delete mytruck;
      return 0;
    }
    Victor Nijegorodov

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Subclass objects in one array?

    Quote Originally Posted by ImNotaBot View Post
    Here again an example:
    Note that the vehicle class must have a virtual destructor if you want to store (and ultimately delete, I presume) derived instances using a pointer to the base class.

    You can also have a look at the Boost Pointer Container Library.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Subclass objects in one array?

    When i remember right, Java and C# offer this feature.
    The method that Java and C# use is similar, under the hood, to what you do in C++.

    There is not actually much difference between the concepts of Java & C++.
    Both have containers that store one type of object.

    In Java, you can only store references to objects in a container.

    In C++ you store copies of objects in a container. The difference being that is that the objects stored could be pointers.

    Code:
    vector<car>    myCars; // A vector of 'car' objects. The 'car's are stored in the vector. No equivalent in java.
    vector<car *> myPointerCars; // A vector of pointers to 'car' objects. The 'car's are stored elsewhere.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #6
    Join Date
    Feb 2012
    Posts
    19

    Re: Subclass objects in one array?

    Well sorry that I did not answer. Quite impolite... but I am terribly under pressure of time due to exams.
    I will come back to the topic as soon as i made them.

    I really appreciate your help so thanks to all of you.

    @ VictorN: Yes your right :-D i just wrote it down for a quick sloppy illustration of the problem.
    I'm hanging around in english forums, becuase i want to _improve_ my english. So please feel free to correct me.

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