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. The Lounge
  3. I see a lot of C++ guys here, So one question please?

I see a lot of C++ guys here, So one question please?

Scheduled Pinned Locked Moved The Lounge
questionc++databasegraphicsdata-structures
10 Posts 6 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
    Masoud Samimi
    wrote on last edited by
    #1

    Hi every body, Nice to be around and have all you nice coders here! :-D O.K., to the question: With C++ we have the benefit of Polymorphysm, right? O.K., In 3D CG ( computer graphics ), When we write a generic base class that would be used to derive from it further classes, i.e. I have a base class called C3DShape and then from it I have derived these: C3DShpere, C3DCube and etc... Then we use a pointers collection array class to collect all the added shapes in a 3D scene, The question is that how can we select and detect what shape has been selected using the array idex of the shape in question? ( to make sure the right selection). In other words if not clear, How can we connect the index from the array to the object of the related shape class , which we need to select? I hope and appreciate help with this? :-( Thanks in advance, Cheers Masoud

    A C L M O 6 Replies Last reply
    0
    • M Masoud Samimi

      Hi every body, Nice to be around and have all you nice coders here! :-D O.K., to the question: With C++ we have the benefit of Polymorphysm, right? O.K., In 3D CG ( computer graphics ), When we write a generic base class that would be used to derive from it further classes, i.e. I have a base class called C3DShape and then from it I have derived these: C3DShpere, C3DCube and etc... Then we use a pointers collection array class to collect all the added shapes in a 3D scene, The question is that how can we select and detect what shape has been selected using the array idex of the shape in question? ( to make sure the right selection). In other words if not clear, How can we connect the index from the array to the object of the related shape class , which we need to select? I hope and appreciate help with this? :-( Thanks in advance, Cheers Masoud

      A Offline
      A Offline
      Andy Hassall
      wrote on last edited by
      #2

      Let me count the ways :-) There's RTTI (Run Time Type Information), which is part of C++ itself. Generation of RTTI information isn't turned on by default in VC++, IIRC. It's also only generated for classes with at least one virtual function (which is almost certain to be true for your setup). You can then use the 'typeid' operator to get the runtime class of an object. Or, you could have a member variable with a value representing the type of the object. MFC classes (deriving from CObject) also have the CRuntimeClass mechanism, which is similar to the typeid method. You're probably not using MFC classes for this, though. Having to know the runtime class of an object isn't exactly very pure OO, though. Surely you can do everything you want with clever use of virtual functions?

      1 Reply Last reply
      0
      • M Masoud Samimi

        Hi every body, Nice to be around and have all you nice coders here! :-D O.K., to the question: With C++ we have the benefit of Polymorphysm, right? O.K., In 3D CG ( computer graphics ), When we write a generic base class that would be used to derive from it further classes, i.e. I have a base class called C3DShape and then from it I have derived these: C3DShpere, C3DCube and etc... Then we use a pointers collection array class to collect all the added shapes in a 3D scene, The question is that how can we select and detect what shape has been selected using the array idex of the shape in question? ( to make sure the right selection). In other words if not clear, How can we connect the index from the array to the object of the related shape class , which we need to select? I hope and appreciate help with this? :-( Thanks in advance, Cheers Masoud

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        The way we do it at work ( exact situation, multiple shapes derived from base class in 3D scene ) is to have a function in the base class that returns type, and set type using an enumerated variable ( so we can switch it if needed ) Christian The content of this post is not necessarily the opinion of my yadda yadda yadda. To understand recursion, we must first understand recursion.

        1 Reply Last reply
        0
        • M Masoud Samimi

          Hi every body, Nice to be around and have all you nice coders here! :-D O.K., to the question: With C++ we have the benefit of Polymorphysm, right? O.K., In 3D CG ( computer graphics ), When we write a generic base class that would be used to derive from it further classes, i.e. I have a base class called C3DShape and then from it I have derived these: C3DShpere, C3DCube and etc... Then we use a pointers collection array class to collect all the added shapes in a 3D scene, The question is that how can we select and detect what shape has been selected using the array idex of the shape in question? ( to make sure the right selection). In other words if not clear, How can we connect the index from the array to the object of the related shape class , which we need to select? I hope and appreciate help with this? :-( Thanks in advance, Cheers Masoud

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I think RTTI may help you in this scenario.

          1 Reply Last reply
          0
          • M Masoud Samimi

            Hi every body, Nice to be around and have all you nice coders here! :-D O.K., to the question: With C++ we have the benefit of Polymorphysm, right? O.K., In 3D CG ( computer graphics ), When we write a generic base class that would be used to derive from it further classes, i.e. I have a base class called C3DShape and then from it I have derived these: C3DShpere, C3DCube and etc... Then we use a pointers collection array class to collect all the added shapes in a 3D scene, The question is that how can we select and detect what shape has been selected using the array idex of the shape in question? ( to make sure the right selection). In other words if not clear, How can we connect the index from the array to the object of the related shape class , which we need to select? I hope and appreciate help with this? :-( Thanks in advance, Cheers Masoud

            M Offline
            M Offline
            Masoud Samimi
            wrote on last edited by
            #5

            Hi, Thanks for all the replies, all are helpful. Andy: I have derived from CObject for serializing methods. It also contains many virtual funcs. I am using the OpenGL process selection technique, how to integrate them together? Using the CRunTimeClass, where excatly? As an idea: C3DShape* ProcessSelection(...) { C3DShape* pShape = NULL; . // Where in here sould we insert the detective code? . return pShape; } Chris: I have the enum variable like this: enum ShapeSelected { SPHERE = 1, CUBE = 2, LIGHT = 3, . . . }; But in the selection process I do not get the index or ID back to my selection buffer, from the CTypePtrArray? What is missing, I don't know? How is the type return method used? -- Some more: I have this in my document class .h and .cpp: This one is for the collections: typedef CTypedPtrArray C3DShape; C3DShape m_pShpArray; This one to return the index from array: C3DShape *CxxxDoc::GetShape (int Index) { if (Index < 0 || Index > m_pShpArray.GetUpperBound ()) return 0; return (C3DShape *)m_pShpArray.GetAt (Index); } Looking to further helps, I appreciate. Cheers Masoud

            A M 2 Replies Last reply
            0
            • M Masoud Samimi

              Hi every body, Nice to be around and have all you nice coders here! :-D O.K., to the question: With C++ we have the benefit of Polymorphysm, right? O.K., In 3D CG ( computer graphics ), When we write a generic base class that would be used to derive from it further classes, i.e. I have a base class called C3DShape and then from it I have derived these: C3DShpere, C3DCube and etc... Then we use a pointers collection array class to collect all the added shapes in a 3D scene, The question is that how can we select and detect what shape has been selected using the array idex of the shape in question? ( to make sure the right selection). In other words if not clear, How can we connect the index from the array to the object of the related shape class , which we need to select? I hope and appreciate help with this? :-( Thanks in advance, Cheers Masoud

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Tools like RTTI and the casting operators are basically nothing more than a cheesey way for people who just screwed up an object oriented design to make their deadline. If your classes are designed with sufficiently generic interfaces the need for such hacks is greatly reduced or eliminated entirely. In other words, it does not matter which index your object is in - all contained objects know how to respond to any possible request.

              P 1 Reply Last reply
              0
              • M Masoud Samimi

                Hi every body, Nice to be around and have all you nice coders here! :-D O.K., to the question: With C++ we have the benefit of Polymorphysm, right? O.K., In 3D CG ( computer graphics ), When we write a generic base class that would be used to derive from it further classes, i.e. I have a base class called C3DShape and then from it I have derived these: C3DShpere, C3DCube and etc... Then we use a pointers collection array class to collect all the added shapes in a 3D scene, The question is that how can we select and detect what shape has been selected using the array idex of the shape in question? ( to make sure the right selection). In other words if not clear, How can we connect the index from the array to the object of the related shape class , which we need to select? I hope and appreciate help with this? :-( Thanks in advance, Cheers Masoud

                O Offline
                O Offline
                Oz Solomon
                wrote on last edited by
                #7

                You don't have many choices, since you're using MFC's CArray. An alternative is the STL vector class. With vectors, if you pass iterators (as opposed to streight pointers), you can always calculate the index. Here's some code, excuse the syntax, since I don't have an editor handy: vector MyArr; // init add some values MyArr.push_back(10); MyArr.push_back(15); MyArr.push_back(20); vector::iterator pInt; pInt = MyArr.at(1); // use an iterator instead of pointer .... int iElement = *pInt; // access "pointer" (actually iterator) int iIndex = pInt - MyArr.begin(); // calculate index I hope this is what you wanted -Oz --- Grab WndTabs from http://www.wndtabs.com to make your VC++ experience that much more comfortable...

                1 Reply Last reply
                0
                • M Masoud Samimi

                  Hi, Thanks for all the replies, all are helpful. Andy: I have derived from CObject for serializing methods. It also contains many virtual funcs. I am using the OpenGL process selection technique, how to integrate them together? Using the CRunTimeClass, where excatly? As an idea: C3DShape* ProcessSelection(...) { C3DShape* pShape = NULL; . // Where in here sould we insert the detective code? . return pShape; } Chris: I have the enum variable like this: enum ShapeSelected { SPHERE = 1, CUBE = 2, LIGHT = 3, . . . }; But in the selection process I do not get the index or ID back to my selection buffer, from the CTypePtrArray? What is missing, I don't know? How is the type return method used? -- Some more: I have this in my document class .h and .cpp: This one is for the collections: typedef CTypedPtrArray C3DShape; C3DShape m_pShpArray; This one to return the index from array: C3DShape *CxxxDoc::GetShape (int Index) { if (Index < 0 || Index > m_pShpArray.GetUpperBound ()) return 0; return (C3DShape *)m_pShpArray.GetAt (Index); } Looking to further helps, I appreciate. Cheers Masoud

                  A Offline
                  A Offline
                  Andy Hassall
                  wrote on last edited by
                  #8

                  I don't know much at all about OpenGL, so I'm not sure what you mean by integrating them together. As for getting the runtime class of the object, since you're using CObjects, and you mentioned serialisation so you probably have the DECLARE_SERIAL(...) macros in use, then you can get the CRuntimeClass object using CObject::GetRuntimeClass. You can then do some comparisons to find which class it is. Of course, you'll need to keep track of all the class names, and alter the routine for each new type of shape. I still say it's messy doing it like this, and I agree with Stan's comment below, it's bad OO design. If the function has a pointer to a C3DShape, then it shouldn't need to know whether it's actually an object of a class derived from it; the object itself should know how to react. That's the whole idea behind virtual functions. - Re-reading your initial message, and what you've said here, then perhaps I've got the wrong end of the stick. Is what you're really after just a way of finding the array index into your shapes array, given a pointer to an object that's in that array? In that case you could do a search through the array for a matching pointer, and return the index. This isn't exactly efficient for large arrays, although I suppose you could keep the array sorted by pointer address, and do a binary search. Another way would be to have a map relating pointer to array index, that you keep updated in sync with the actual array contents. Something like the CMap class in MFC; I tend to use STL rather than the MFC containers so I don't know whether this is the most suitable MFC class. Hope it helps, and that I've not got it all confused :-)

                  1 Reply Last reply
                  0
                  • L Lost User

                    Tools like RTTI and the casting operators are basically nothing more than a cheesey way for people who just screwed up an object oriented design to make their deadline. If your classes are designed with sufficiently generic interfaces the need for such hacks is greatly reduced or eliminated entirely. In other words, it does not matter which index your object is in - all contained objects know how to respond to any possible request.

                    P Offline
                    P Offline
                    Paul Wolfensberger
                    wrote on last edited by
                    #9

                    I don't know if I entirely agree....I've found a case where I have a pointer to an object. There is a virtual method which I'd like to bypass in order to get to a base class method, but because its a virtual I can't if a derived class is passed to me....so I added a "bypass" method to the derived class, determine if that class was passed to me, and if it is, I call a different method. The second case where RTTI SHOULD be used: Multiple Inheritance. In order to determine if a given base class (or "interface") is supported, you should use one of the RTTI casting methods to determine if the object is of the appropriate type.

                    1 Reply Last reply
                    0
                    • M Masoud Samimi

                      Hi, Thanks for all the replies, all are helpful. Andy: I have derived from CObject for serializing methods. It also contains many virtual funcs. I am using the OpenGL process selection technique, how to integrate them together? Using the CRunTimeClass, where excatly? As an idea: C3DShape* ProcessSelection(...) { C3DShape* pShape = NULL; . // Where in here sould we insert the detective code? . return pShape; } Chris: I have the enum variable like this: enum ShapeSelected { SPHERE = 1, CUBE = 2, LIGHT = 3, . . . }; But in the selection process I do not get the index or ID back to my selection buffer, from the CTypePtrArray? What is missing, I don't know? How is the type return method used? -- Some more: I have this in my document class .h and .cpp: This one is for the collections: typedef CTypedPtrArray C3DShape; C3DShape m_pShpArray; This one to return the index from array: C3DShape *CxxxDoc::GetShape (int Index) { if (Index < 0 || Index > m_pShpArray.GetUpperBound ()) return 0; return (C3DShape *)m_pShpArray.GetAt (Index); } Looking to further helps, I appreciate. Cheers Masoud

                      M Offline
                      M Offline
                      Masoud Samimi
                      wrote on last edited by
                      #10

                      Hi all, Thanks to all, you are all helpful, very nice. :-) Dear Andy and Stan , I think you guys are absolutely right :), I did miss somethings in the virtuals. I'll try fixing some if not all and see if it comes out allright. In case otherwise, I'll post further to fix this nightmare! :-( One last comment though, if I am going to check for each type return and compare of the CRunTimeClass method, I would end up writing two or three packed A4 pages of code list! :eek: , coz I've got about 30 items or more of 2D and 3D class shapes derived from the base class. :rolleyes:, this is why I chose the CTypedPtrArray for pointers collect instead of CObList and manual add, remove, etc... Cheers Masoud

                      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