copying a vector of objects at runtime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Julian

    copying a vector of objects at runtime

    Hi,
    I have a vector defined like this:
    std::vector<Loa d*LoadList;
    which is populated with different objects of classes that are derived from
    'Load'.
    I need to make a copy of this list during runtime.
    I imagine I'd have to loop through the list, make a copy of each of the
    objects in the vector using the copy constructor... but I don't know the
    class name during run time so that I can call the appropriate copy
    constructor.
    actually, I read up on 'typeid' which I supposed would give me the class
    name... but I am still confused as how I can call the appropriate copy
    constructor for that class during run time.
    One option would be to do a string compare of the class names... but that
    requires for me to be aware of all the classes derived from 'Load'.
    Is there some other way to go about this?
    thanks,
    Julian.


  • Julian

    #2
    Re: copying a vector of objects at runtime


    "Julian" <julvar@nospamt amu.eduwrote in message
    news:g54akk$c32 $1@news.tamu.ed u...
    Hi,
    I have a vector defined like this:
    std::vector<Loa d*LoadList;
    which is populated with different objects of classes that are derived from
    'Load'.
    I need to make a copy of this list during runtime.
    I imagine I'd have to loop through the list, make a copy of each of the
    objects in the vector using the copy constructor... but I don't know the
    class name during run time so that I can call the appropriate copy
    constructor.
    actually, I read up on 'typeid' which I supposed would give me the class
    name... but I am still confused as how I can call the appropriate copy
    constructor for that class during run time.
    One option would be to do a string compare of the class names... but that
    requires for me to be aware of all the classes derived from 'Load'.
    Is there some other way to go about this?
    thanks,
    Julian.
    I did some more searching and looks like I found the solution from a 10 yr
    old post!

    do let me know if this isn't the right solution
    sorry for the inconvenience


    Comment

    • Joe Greer

      #3
      Re: copying a vector of objects at runtime

      "Julian" <julvar@nospamt amu.eduwrote in
      news:g54akk$c32 $1@news.tamu.ed u:
      Hi,
      I have a vector defined like this:
      std::vector<Loa d*LoadList;
      which is populated with different objects of classes that are derived
      from 'Load'.
      I need to make a copy of this list during runtime.
      I imagine I'd have to loop through the list, make a copy of each of
      the objects in the vector using the copy constructor... but I don't
      know the class name during run time so that I can call the appropriate
      copy constructor.
      Hmmm, if copying the objects in the vector is the answer, then I have to
      wonder why there are pointers in the vector to begin with. That is,
      wouldn't vector<Loadhave been a better place to start? Then copying
      would be as simple as LoadList2 = LoadList. IME the only time you
      really want a vector of pointers is if the objects have identity
      (can't/shouldn't be copied at all) or are horrendously expensive to
      copy.

      If you still want to copy the vector of pointers (after considering
      making them value types instead), you can do something like:

      #include <iostream>
      #include <sstream>
      #include <vector>

      template <typename T>
      T * clone(T * a)
      {
      return new T(*a); //assuming copy constructor
      }

      template <typename T>
      class clone_insert_it erator : public std::iterator
      <std::output_it erator_tag, typename T::value_type>
      {
      T& m_Container;
      public:
      explicit clone_insert_it erator(T & container) : m_Container
      (container) {}
      clone_insert_it erator<T& operator=(typen ame T::value_type const &
      a)
      {
      m_Container.pus h_back(clone(a) );
      return *this;
      }
      clone_insert_it erator<T& operator*()
      {
      return *this;
      }
      clone_insert_it erator<T& operator++()
      {
      return *this;
      }
      clone_insert_it erator<Toperato r++(int)
      {
      return *this;
      }
      };


      int main()
      {
      std::vector<int *v;
      std::vector<int *v1;

      v.push_back(new int(5));
      v.push_back(new int(1));
      v.push_back(new int(10));
      v.push_back(new int(7));

      clone_insert_it erator<std::vec tor<int * cii(v1);
      std::copy(v.beg in(), v.end(), cii);

      for (int ix = 0; ix < v1.size(); ++ix)
      {
      std::cout << *v[ix] << std::endl;
      }
      std::cout << std::endl;
      std::cout << std::endl;
      for (int ix = 0; ix < v1.size(); ++ix)
      {
      std::cout << *v1[ix] << std::endl;
      }
      }


      Which seems to work, but I made no attempt at robustness nor
      completeness for any particular use.


      joe

      Comment

      • Daniel T.

        #4
        Re: copying a vector of objects at runtime

        "Julian" <julvar@nospamt amu.eduwrote:
        "Julian" <julvar@nospamt amu.eduwrote:
        I have a vector defined like this:
        std::vector<Loa d*LoadList;
        which is populated with different objects of classes that are derived from
        'Load'.
        I need to make a copy of this list during runtime.
        I imagine I'd have to loop through the list, make a copy of each of the
        objects in the vector using the copy constructor... but I don't know the
        class name during run time so that I can call the appropriate copy
        constructor.
        actually, I read up on 'typeid' which I supposed would give me the class
        name... but I am still confused as how I can call the appropriate copy
        constructor for that class during run time.
        One option would be to do a string compare of the class names... but that
        requires for me to be aware of all the classes derived from 'Load'.
        Is there some other way to go about this?
        thanks,
        Julian.
        >
        I did some more searching and looks like I found the solution from a 10 yr
        old post!

        35d0/ea75e39ab5004cf c?lnk=st&q=copy ing+objects+c%2 B%2B#
        do let me know if this isn't the right solution
        sorry for the inconvenience
        Yes, this is an FAQ:



        Be advised though, this may not be the best solution for you. You should
        also look into holding a vector of smart pointers. If that will work for
        you (i.e., if it's ok if both vectors actually point to the same set of
        objects,) then copying will be a simple matter of assignment:

        vector<smart_pt r<Load loadList;
        vector<smart_pt r<Load copy = loadList;

        Comment

        • spacemanspiff

          #5
          Re: copying a vector of objects at runtime

          On Jul 10, 9:46 pm, "Daniel T." <danie...@earth link.netwrote:
          "Julian" <jul...@nospamt amu.eduwrote:
          "Julian" <jul...@nospamt amu.eduwrote:
          I have avectordefined like this:
          std::vector<Loa d*LoadList;
          which is populated with differentobject sof classes that are derived from
          'Load'.
          I need to make a copy of this list during runtime.
          I imagine I'd have to loop through the list, make a copy of each of the
          >objectsin thevectorusing the copy constructor... but I don't know the
          class name during run time so that I can call the appropriate copy
          constructor.
          actually, I read up on 'typeid' which I supposed would give me the class
          name... but I am still confused as how I can call the appropriate copy
          constructor for that class during run time.
          One option would be to do a string compare of the class names... but that
          requires for me to be aware of all the classes derived from 'Load'.
          Is there some other way to go about this?
          thanks,
          Julian.
          >
          I did some more searching and looks like I found the solution from a 10yr
          old post!
          http://groups.google.com/group/comp....ead/thread/1d4...
          35d0/ea75e39ab5004cf c?lnk=st&q=copy ing+objects+c%2 B%2B#
          do let me know if this isn't the right solution
          sorry for the inconvenience
          >
          Yes, this is an FAQ:
          >

          >
          Be advised though, this may not be the best solution for you. You should
          also look into holding avectorof smart pointers. If that will work for
          you (i.e., if it's ok if both vectors actually point to the same set ofobjects,) thencopyingwill be a simple matter of assignment:
          >
          vector<smart_pt r<Load loadList;vector <smart_ptr<Lo ad copy = loadList;
          thanks for the response...I will look into smart pointers. but in this
          case, I actually want to create a copy of the set of objects. not just
          a set of pointers that point to the original set of objects.
          I am trying to parallelize some of the steps in my Finite element
          analysis program and the reasoning for doing this is so that each
          processor can have its own set of objects so that it doesn't cause
          conflicts with what the other processor is doing. hope that makes
          sense.

          Julian.

          Comment

          • Julian

            #6
            Re: copying a vector of objects at runtime

            On Jul 10, 11:35 pm, spacemanspiff <jul...@gmail.c omwrote:
            On Jul 10, 9:46 pm, "Daniel T." <danie...@earth link.netwrote:
            >
            >
            >
            "Julian" <jul...@nospamt amu.eduwrote:
            "Julian" <jul...@nospamt amu.eduwrote:
            I have avectordefined like this:
            std::vector<Loa d*LoadList;
            which is populated with differentobject sof classes that are derivedfrom
            'Load'.
            I need to make a copy of this list during runtime.
            I imagine I'd have to loop through the list, make a copy of each ofthe
            objectsin thevectorusing the copy constructor... but I don't know the
            class name during run time so that I can call the appropriate copy
            constructor.
            actually, I read up on 'typeid' which I supposed would give me the class
            name... but I am still confused as how I can call the appropriate copy
            constructor for that class during run time.
            One option would be to do a string compare of the class names... but that
            requires for me to be aware of all the classes derived from 'Load'.
            Is there some other way to go about this?
            thanks,
            Julian.
            >
            I did some more searching and looks like I found the solution from a 10 yr
            old post!
            >http://groups.google.com/group/comp....ead/thread/1d4....
            35d0/ea75e39ab5004cf c?lnk=st&q=copy ing+objects+c%2 B%2B#
            do let me know if this isn't the right solution
            sorry for the inconvenience
            >
            Yes, this is an FAQ:
            >>
            Be advised though, this may not be the best solution for you. You should
            also look into holding avectorof smart pointers. If that will work for
            you (i.e., if it's ok if both vectors actually point to the same set ofobjects,) thencopyingwill be a simple matter of assignment:
            >
            vector<smart_pt r<Load loadList;vector <smart_ptr<Lo ad copy = loadList;
            >
            thanks for the response...I will look into smart pointers. but in this
            case, I actually want to create a copy of the set of objects. not just
            a set of pointers that point to the original set of objects.
            I am trying to parallelize some of the steps in my Finite element
            analysis program and the reasoning for doing this is so that each
            processor can have its own set of objects so that it doesn't cause
            conflicts with what the other processor is doing. hope that makes
            sense.
            >
            Julian.
            Sorry for the confusion... i am the original poster. I just started
            using Google groups now

            Comment

            • Julian

              #7
              Re: copying a vector of objects at runtime

              On Jul 10, 10:02 am, Joe Greer <jgr...@doublet ake.comwrote:
              "Julian" <jul...@nospamt amu.eduwrote innews:g54akk$c 32$1@news.tamu. edu:
              >
              Hi,
              I have a vector defined like this:
              std::vector<Loa d*LoadList;
              which is populated with different objects of classes that are derived
              from 'Load'.
              I need to make a copy of this list during runtime.
              I imagine I'd have to loop through the list, make a copy of each of
              the objects in the vector using the copy constructor... but I don't
              know the class name during run time so that I can call the appropriate
              copy constructor.
              >
              Hmmm, if copying the objects in the vector is the answer, then I have to
              wonder why there are pointers in the vector to begin with.  That is,
              wouldn't vector<Loadhave been a better place to start?  Then copying
              would be as simple as LoadList2 = LoadList.  IME the only time you
              really want a vector of pointers is if the objects have identity
              (can't/shouldn't be copied at all) or are horrendously expensive to
              copy.
              >
              I'm not really sure if I understand the first part of your response.
              from what I understand (and please let me know if i'm on the wrong
              track), vector<Loadcan only store only Load objects. even if i try
              to store an object of a class derived from Load, it still stores only
              a Load object because it calls Load(const Load&) when you do a
              push_back.
              In my case, I want this vector to 'store' a combination of Load
              objects AND other objects that are derived from Load.
              hope this helps in describing my situation... and please let me know
              if it better for me to use some other c++ strategy.
              thanks,
              Julian.

              Comment

              • red floyd

                #8
                Re: copying a vector of objects at runtime

                Joe Greer wrote:
                "Julian" <julvar@nospamt amu.eduwrote in
                news:g54akk$c32 $1@news.tamu.ed u:
                >
                >Hi,
                >I have a vector defined like this:
                >std::vector<Lo ad*LoadList;
                >which is populated with different objects of classes that are derived
                >from 'Load'.
                >I need to make a copy of this list during runtime.
                >I imagine I'd have to loop through the list, make a copy of each of
                >the objects in the vector using the copy constructor... but I don't
                >know the class name during run time so that I can call the appropriate
                >copy constructor.
                >
                Hmmm, if copying the objects in the vector is the answer, then I have to
                wonder why there are pointers in the vector to begin with. That is,
                wouldn't vector<Loadhave been a better place to start? Then copying
                would be as simple as LoadList2 = LoadList. IME the only time you
                really want a vector of pointers is if the objects have identity
                (can't/shouldn't be copied at all) or are horrendously expensive to
                copy.
                >
                You forgot the other case, where your vector contains polymorphic
                objects. But then you should probably be using a container of your
                favorite smart pointer.

                Comment

                • Joe Greer

                  #9
                  Re: copying a vector of objects at runtime

                  Julian <julvar@gmail.c omwrote in
                  news:00c0d939-e3d2-4c2b-85b3-f49efec35285@59 g2000hsb.google groups.com:
                  On Jul 10, 10:02 am, Joe Greer <jgr...@doublet ake.comwrote:
                  >"Julian" <jul...@nospamt amu.eduwrote
                  >innews:g54akk$ c32$1@news.tamu .edu:
                  >>
                  >
                  I'm not really sure if I understand the first part of your response.
                  from what I understand (and please let me know if i'm on the wrong
                  track), vector<Loadcan only store only Load objects. even if i try
                  to store an object of a class derived from Load, it still stores only
                  a Load object because it calls Load(const Load&) when you do a
                  push_back.
                  In my case, I want this vector to 'store' a combination of Load
                  objects AND other objects that are derived from Load.
                  hope this helps in describing my situation... and please let me know
                  if it better for me to use some other c++ strategy.
                  thanks,
                  Julian.
                  Ah yes, I forgot polymorphism... In that case, if your objects have
                  identity, that is, you really only want one instance of it, then you might
                  look at a smart_ptr (from either TR1 or boost) to manage lifetime of the
                  object and just copy the vector normally. Or you make your Load interface
                  contain a clone() method of some sort which has the object create a copy of
                  itself. A clone_inserter iterator can then be made which invokes this
                  method to get the clone of the object and use std::copy() as I described
                  elsewhere.

                  joe

                  Comment

                  • Joe Greer

                    #10
                    Re: copying a vector of objects at runtime

                    red floyd <no.spam.here@e xample.comwrote in
                    news:qiCdk.1209 1$cW3.7840@nlpi 064.nbdc.sbc.co m:
                    Joe Greer wrote:
                    >"Julian" <julvar@nospamt amu.eduwrote in
                    >news:g54akk$c3 2$1@news.tamu.e du:
                    >>
                    >>Hi,
                    >>I have a vector defined like this:
                    >>std::vector<L oad*LoadList;
                    >>which is populated with different objects of classes that are
                    >>derived from 'Load'.
                    >>I need to make a copy of this list during runtime.
                    >>I imagine I'd have to loop through the list, make a copy of each of
                    >>the objects in the vector using the copy constructor... but I don't
                    >>know the class name during run time so that I can call the
                    >>appropriate copy constructor.
                    >>
                    >Hmmm, if copying the objects in the vector is the answer, then I have
                    >to wonder why there are pointers in the vector to begin with. That
                    >is, wouldn't vector<Loadhave been a better place to start? Then
                    >copying would be as simple as LoadList2 = LoadList. IME the only
                    >time you really want a vector of pointers is if the objects have
                    >identity (can't/shouldn't be copied at all) or are horrendously
                    >expensive to copy.
                    >>
                    >
                    You forgot the other case, where your vector contains polymorphic
                    objects. But then you should probably be using a container of your
                    favorite smart pointer.
                    Yep. Somehow, the way the question was worded, It didn't occur to me that
                    these were pointers to a base/interface. Sigh.

                    joe

                    Comment

                    Working...