Thread: Can a vector hold multiple objects of a different type?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Can a vector hold multiple objects of a different type?

    If a vector can hold multiple objects of a different type, then how can one distinguish between them?

    If it can't, then what is recommended to allow the placement of different object types into a single structure that may be referenced at any time and the object's type can be inferred and understood?

    I am not referring to a union although I know it will do the job but not in a way I desire.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    AFAIK C++ does not have any truly heterogeneous data structures.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    but you can have a vector of base class pointers, and put derived class pointers in it.

    boost::any might be a possibility if you need more than that.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You can use a combination of `boost::any', as an abstract function, and function templates to do pretty much everything you might wish to do with a heterogeneous container.

    What are you trying to do? (Note: This is a very different beast from "How are you trying to do it?".)

    Soma

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Directly or indirectly, you can put anything you like inside a vector. The only hard part is figuring out how you are going to represent the variant type.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by phantomotap View Post
    O_o

    What are you trying to do? (Note: This is a very different beast from "How are you trying to do it?".)

    Soma
    As I wrote, I would like to place objects of different class types into a single vector. The vector will be of the type of the base class and, as was noted above, the derived classes would place a pointer to a derived class object inside the vector thus enabling the vector to take different types.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Then you'll not need to do "any" vodoo!
    Make good use of virtual functions to do away with any type inference required.
    Also, the items must be pointers or references, otherwise they'll be spliced . [EDIT: sliced, not spliced !]

    This is what OOP is almost all about, so make sure you understand it thoroughly.
    Also google "Liskov Substitution Principle".
    Last edited by manasij7479; 01-21-2013 at 09:36 AM.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by manasij7479 View Post
    otherwise they'll be spliced.
    maybe you just mistyped it, but it's important to note that the term we usually use is "sliced." as in the data implemented by the derived class(es) get "sliced" off, leaving only the base. in this situation, the derived object is not recoverable.

    spliced implies exactly the opposite concept.

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elkvis View Post
    maybe you just mistyped it, but it's important to note that the term we usually use is "sliced." as in the data implemented by the derived class(es) get "sliced" off, leaving only the base. in this situation, the derived object is not recoverable.

    spliced implies exactly the opposite concept.
    Thought they were the same !(I had an idea that splice came from splinter .)
    Thanks for making my vocabulary less ambigious.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Actually comes from Middle Dutch, from a verb meaning "to split" apparently. Odd. Splice - Definition and More from the Free Merriam-Webster Dictionary

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manasij7479 View Post
    Then you'll not need to do "any" vodoo!
    Make good use of virtual functions to do away with any type inference required.
    Also, the items must be pointers or references, otherwise they'll be spliced . [EDIT: sliced, not spliced !]

    This is what OOP is almost all about, so make sure you understand it thoroughly.
    Also google "Liskov Substitution Principle".
    You obviously can't store references in a vector...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by manasij7479 View Post
    Thought they were the same !(I had an idea that splice came from splinter .)
    Thanks for making my vocabulary less ambigious.
    based on whiteflags's post, it appears that splinter and splice could be related, since they both came from middle dutch - Splinter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Setting up a Class to hold Objects with multiple attributes
    By IPthereforIam in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2012, 10:50 AM
  2. How to instantiate multiple objects C
    By zahid990170 in forum C Programming
    Replies: 5
    Last Post: 06-15-2011, 10:26 AM
  3. Question about multiple objects with the same name
    By shiroaisu in forum C++ Programming
    Replies: 9
    Last Post: 06-07-2010, 06:03 AM
  4. C array to hold multiple structs?
    By zerointeger in forum C Programming
    Replies: 5
    Last Post: 11-30-2009, 04:58 PM
  5. Vector of pointers to vector of objects
    By Litz in forum C++ Programming
    Replies: 10
    Last Post: 11-06-2009, 03:29 PM