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