Like Tree2Likes
  • 1 Post By phantomotap
  • 1 Post By Elkvis

Can a vector hold multiple objects of a different type?

This is a discussion on Can a vector hold multiple objects of a different type? within the C++ Programming forums, part of the General Programming Boards category; If a vector can hold multiple objects of a different type, then how can one distinguish between them? If it ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    AFAIK C++ does not have any truly heterogeneous data structures.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    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
    4,415
    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
    King Mir likes this.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    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.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    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.
    manasij7479 likes this.

  9. #9
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    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.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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
    Posts
    23,014
    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
    2,588
    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, 11:50 AM
  2. How to instantiate multiple objects C
    By zahid990170 in forum C Programming
    Replies: 5
    Last Post: 06-15-2011, 11:26 AM
  3. Question about multiple objects with the same name
    By shiroaisu in forum C++ Programming
    Replies: 9
    Last Post: 06-07-2010, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21