Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. STL Lists and base/derived classes

STL Lists and base/derived classes

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++algorithmsquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mjackson11
    wrote on last edited by
    #1

    I have four or five kinds of objects, all derived from an indexedObject. indexedObject's have an integer id which can be set and they can be compared with <, >, etc to facilitate sorting, pretty simple class. I would like to create one class that wraps a list and adds some other functionality and can hold all these kinds of objects. Problem is if I use a std::list to hold the objects, I lose access to the functions not in indexedObject. I would store pointers but then sorting and looking up the objects would require lots of extra code. Is there a way to use the std::list list to store the objects and cast them back to their derived type as appropriate? I can't use the assignment operator, I get an error. I thought about all kinds of things like memcpy or something but it seems as if I resort to that I am getting pretty far off the reservation. Any suggestions?

    C C M 3 Replies Last reply
    0
    • M mjackson11

      I have four or five kinds of objects, all derived from an indexedObject. indexedObject's have an integer id which can be set and they can be compared with <, >, etc to facilitate sorting, pretty simple class. I would like to create one class that wraps a list and adds some other functionality and can hold all these kinds of objects. Problem is if I use a std::list to hold the objects, I lose access to the functions not in indexedObject. I would store pointers but then sorting and looking up the objects would require lots of extra code. Is there a way to use the std::list list to store the objects and cast them back to their derived type as appropriate? I can't use the assignment operator, I get an error. I thought about all kinds of things like memcpy or something but it seems as if I resort to that I am getting pretty far off the reservation. Any suggestions?

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Is RTTI [^] what are you looking for? :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      1 Reply Last reply
      0
      • M mjackson11

        I have four or five kinds of objects, all derived from an indexedObject. indexedObject's have an integer id which can be set and they can be compared with <, >, etc to facilitate sorting, pretty simple class. I would like to create one class that wraps a list and adds some other functionality and can hold all these kinds of objects. Problem is if I use a std::list to hold the objects, I lose access to the functions not in indexedObject. I would store pointers but then sorting and looking up the objects would require lots of extra code. Is there a way to use the std::list list to store the objects and cast them back to their derived type as appropriate? I can't use the assignment operator, I get an error. I thought about all kinds of things like memcpy or something but it seems as if I resort to that I am getting pretty far off the reservation. Any suggestions?

        C Offline
        C Offline
        Code o mat
        wrote on last edited by
        #3

        If i understood you right, what you are doing is a bit dangerous... however, if you are sure about what you are doing, you could try simply get the address of the certain item and convert it to a pointer of the right class and access it thorough that pointer, so what i mean is something like this:

        ((CIndexedObjectDerivant *)(&list_of_CIndexedObjects[12]))->MyRequiredMethod();

        Good luck and marry xmass!

        > The problem with computers is that they do what you tell them to do and not what you want them to do. <

        1 Reply Last reply
        0
        • M mjackson11

          I have four or five kinds of objects, all derived from an indexedObject. indexedObject's have an integer id which can be set and they can be compared with <, >, etc to facilitate sorting, pretty simple class. I would like to create one class that wraps a list and adds some other functionality and can hold all these kinds of objects. Problem is if I use a std::list to hold the objects, I lose access to the functions not in indexedObject. I would store pointers but then sorting and looking up the objects would require lots of extra code. Is there a way to use the std::list list to store the objects and cast them back to their derived type as appropriate? I can't use the assignment operator, I get an error. I thought about all kinds of things like memcpy or something but it seems as if I resort to that I am getting pretty far off the reservation. Any suggestions?

          M Offline
          M Offline
          mjackson11
          wrote on last edited by
          #4

          FWIW, I was able to do the following - std::list _listA; std::list::iterator _iA; std::list> _listB; std::list>::iterator _iB; dObj * p; int i; dObj o; for (i = 5; i > 0; --i) { o.setID(i); // method in base class (iObj) o.setValue(i); // method in derived class (dObj) _listA.push_back(o); _listB.push_back(o); } _listA.sort(); _listB.sort(); _iA = _listA.begin(); _iB = _listB.begin(); i = (*_iB).getValue(); // works i = _iB->getValue(); // works although it's not necessarily defined? p = static_cast *>(&(*_iA)); i = p->getValue(); // works _iA++; p = static_cast *>(&(*_iA)); i = p->getValue(); // WRONG answer. The id associated with iObj was correct (2) but the //member variables associated with the derived object have vanished. return 0; Guess I will just go with sub classing several different kinds of lists.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups