![]() |
|
Welcome to the Computer Webmaster Gaming Console Graphics Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| |||||||
| Software Programming Software programming talk, ask questions about computer software programming or help others |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 | ||
| I had this idea that I could make an ObjectAnimator class, that would animate objects that were derived from an AnimatedObject class. The ObjectAnimator keeps a list of AnimatedObjects, and called their virtual Animate() function on a timed basis. Ok, well that works fine, but the problem comes when I go to take the AnimatedObject off the animate list. The AnimatedObject has a pointer in it, pAnimListNode, to a list node in the animate list, that gets set by the ObjectAnimator when the object is put on the animate list. But - and this is really frustrating, I must be doing something simple wrong - as soon as execution leaves the ObjectAnimator function which sets that pointer, the pointer somehow immediately takes on a different value! Or maybe it is reverting to it's previous value. It's some kind of class inheritance problem. I've tried making AnimatedObject an indirect base, a direct base, virtual public.. nothing I do seems to fix it. Why doesn't this work? What am I doing wrong? If I have, say, class GameObject : virtual public AnimatedObject { void *pAnimListNode; virtual void Animate(); .. .. } ...and I call pObjectAnimator->AddToAnimateList(pGameObject), inside the fuction, pGameObject->pAnimListNode appears to get set properly, but as soon execution leaves the function, the value changes! I think the problem is that ((AnimatedObject *)pGameObject)->m_pAnimListNode is somehow different from ((GameObject *)pGameObject)->m_pAnimListNode, even though by itself GameObject has no such member, and even though AnimatedObject is a virtual public base class. How frustrating. I even made a virtual function in AnimatedObject to set the pointer, overrode that function in GameObject, and made sure that GameObject's version of the function was getting called by ObjectAnimator::AddToAnimateList(). It was, it set the value. The value stuck between the call and the function, but still again, as soon as the thing comes out of the function that sets that value, it reverts. AArgh! I mean, I do a step and trace, and watch the value, and as soon as it comes out, it reverts, so it's not like my program is explicitly doing something to the value somewhere. Any ideas on why this could be happening / how I can fix it? ~Shavais p.s. I'm sure I can get around the problem by passing the correct value for the pointer out of the function and Then setting the value, from a calling function where the derived class is known. But that defeats the whole concept. Why am I even using OOP if I'm going to do that? I should be able to set Lots of base class members in functions where the derived class isn't known, shouldn't I? Without the calling environment knowing or caring about it. What do I have to do, something like... cBaseClassManipulator.ManipulatingFunction( pMyObject , pMyObject->BaseClassStructure ) ... every time? Where BaseClassStructure is a member of the base class of pMyObject that cBaseClassManipulator knows about? How silly is that!? Sorry. Anyway, any help would sure be great. oops actually it's not a direct base class, it's a base class of the base class of the base class. But still. Inheritance is supposed to work, right? lol | |||
| Advertisements |
| | #2 | ||
| BTW yep, I proved my fear is true: After calling BaseClassManipulator.BaseClassMemberChanger(pObjec t), if I look at ((BaseClass *)pObject)->m_nValue, I get the correct result. And the other members of ((BaseClass *)pObject) look fine, too. But if I look at ((DerivedClass *)pObject)->m_nValue, I get an incorrect result! Even though pObject was created as a DerivedClass object. Not only that, but after calling BassClassMemberChanger(), other members of ((DerivedClass *)pObject) are now screwed up too. Oh what fun. Well, so much for my grand plans. ~Shavais "Shavais" <shavais@hotmail.com> wrote in message news:1Pbfb.206286$mp.128903@rwcrnsc51.ops.asp.att. net... > I had this idea that I could make an ObjectAnimator class, that would > animate objects that were derived from an AnimatedObject class. The > ObjectAnimator keeps a list of AnimatedObjects, and called their virtual > Animate() function on a timed basis. Ok, well that works fine, but the > problem comes when I go to take the AnimatedObject off the animate list. > The AnimatedObject has a pointer in it, pAnimListNode, to a list node in the > animate list, that gets set by the ObjectAnimator when the object is put on > the animate list. But - and this is really frustrating, I must be doing > something simple wrong - as soon as execution leaves the ObjectAnimator > function which sets that pointer, the pointer somehow immediately takes on a > different value! Or maybe it is reverting to it's previous value. It's > some kind of class inheritance problem. I've tried making AnimatedObject an > indirect base, a direct base, virtual public.. nothing I do seems to fix it. > Why doesn't this work? What am I doing wrong? > > If I have, say, > > class GameObject : virtual public AnimatedObject { > void *pAnimListNode; > virtual void Animate(); > . > . > } > > ..and I call pObjectAnimator->AddToAnimateList(pGameObject), inside the > fuction, > pGameObject->pAnimListNode appears to get set properly, but as soon > execution leaves the function, the value changes! > > I think the problem is that ((AnimatedObject *)pGameObject)->m_pAnimListNode > is somehow different from ((GameObject *)pGameObject)->m_pAnimListNode, even > though by itself GameObject has no such member, and even though > AnimatedObject is a virtual public base class. How frustrating. > > I even made a virtual function in AnimatedObject to set the pointer, > overrode that function in GameObject, and made sure that GameObject's > version of the function was getting called by > ObjectAnimator::AddToAnimateList(). It was, it set the value. The value > stuck between the call and the function, but still again, as soon as the > thing comes out of the function that sets that value, it reverts. AArgh! > I mean, I do a step and trace, and watch the value, and as soon as it comes > out, it reverts, so it's not like my program is explicitly doing something > to the value somewhere. > > Any ideas on why this could be happening / how I can fix it? > > ~Shavais > > p.s. I'm sure I can get around the problem by passing the correct value for > the pointer out of the function and Then setting the value, from a calling > function where the derived class is known. But that defeats the whole > concept. Why am I even using OOP if I'm going to do that? I should be able > to set Lots of base class members in functions where the derived class isn't > known, shouldn't I? Without the calling environment knowing or caring about > it. What do I have to do, something like... > > cBaseClassManipulator.ManipulatingFunction( pMyObject , > pMyObject->BaseClassStructure ) > > .. every time? Where BaseClassStructure is a member of the base class of > pMyObject that cBaseClassManipulator knows about? How silly is that!? > Sorry. Anyway, any help would sure be great. > > oops actually it's not a direct base class, it's a base class of the base > class of the base class. But still. Inheritance is supposed to work, > right? lol > > > > | |||
| | #3 | ||
| Ok I finally figured this out. This is why they make data members private, and make all those get and set functions. Seems like a lot of trouble to go to to me, but it does appear to fix / prevent this problem. "Shavais" <shavais@hotmail.com> wrote in message news:oRdfb.395819$2x.133866@rwcrnsc52.ops.asp.att. net... > BTW yep, I proved my fear is true: > > After calling BaseClassManipulator.BaseClassMemberChanger(pObjec t), > > if I look at ((BaseClass *)pObject)->m_nValue, > > I get the correct result. And the other members of ((BaseClass *)pObject) > look fine, too. But if I look at > > ((DerivedClass *)pObject)->m_nValue, > > I get an incorrect result! Even though pObject was created as a > DerivedClass object. Not only that, but after calling > BassClassMemberChanger(), other members of ((DerivedClass *)pObject) are now > screwed up too. Oh what fun. Well, so much for my grand plans. > > ~Shavais > > > "Shavais" <shavais@hotmail.com> wrote in message > news:1Pbfb.206286$mp.128903@rwcrnsc51.ops.asp.att. net... > > I had this idea that I could make an ObjectAnimator class, that would > > animate objects that were derived from an AnimatedObject class. The > > ObjectAnimator keeps a list of AnimatedObjects, and called their virtual > > Animate() function on a timed basis. Ok, well that works fine, but the > > problem comes when I go to take the AnimatedObject off the animate list. > > The AnimatedObject has a pointer in it, pAnimListNode, to a list node in > the > > animate list, that gets set by the ObjectAnimator when the object is put > on > > the animate list. But - and this is really frustrating, I must be doing > > something simple wrong - as soon as execution leaves the ObjectAnimator > > function which sets that pointer, the pointer somehow immediately takes on > a > > different value! Or maybe it is reverting to it's previous value. It's > > some kind of class inheritance problem. I've tried making AnimatedObject > an > > indirect base, a direct base, virtual public.. nothing I do seems to fix > it. > > Why doesn't this work? What am I doing wrong? > > > > If I have, say, > > > > class GameObject : virtual public AnimatedObject { > > void *pAnimListNode; > > virtual void Animate(); > > . > > . > > } > > > > ..and I call pObjectAnimator->AddToAnimateList(pGameObject), inside the > > fuction, > > pGameObject->pAnimListNode appears to get set properly, but as soon > > execution leaves the function, the value changes! > > > > I think the problem is that ((AnimatedObject > *)pGameObject)->m_pAnimListNode > > is somehow different from ((GameObject *)pGameObject)->m_pAnimListNode, > even > > though by itself GameObject has no such member, and even though > > AnimatedObject is a virtual public base class. How frustrating. > > > > I even made a virtual function in AnimatedObject to set the pointer, > > overrode that function in GameObject, and made sure that GameObject's > > version of the function was getting called by > > ObjectAnimator::AddToAnimateList(). It was, it set the value. The value > > stuck between the call and the function, but still again, as soon as the > > thing comes out of the function that sets that value, it reverts. AArgh! > > I mean, I do a step and trace, and watch the value, and as soon as it > comes > > out, it reverts, so it's not like my program is explicitly doing something > > to the value somewhere. > > > > Any ideas on why this could be happening / how I can fix it? > > > > ~Shavais > > > > p.s. I'm sure I can get around the problem by passing the correct value > for > > the pointer out of the function and Then setting the value, from a calling > > function where the derived class is known. But that defeats the whole > > concept. Why am I even using OOP if I'm going to do that? I should be > able > > to set Lots of base class members in functions where the derived class > isn't > > known, shouldn't I? Without the calling environment knowing or caring > about > > it. What do I have to do, something like... > > > > cBaseClassManipulator.ManipulatingFunction( pMyObject , > > pMyObject->BaseClassStructure ) > > > > .. every time? Where BaseClassStructure is a member of the base class of > > pMyObject that cBaseClassManipulator knows about? How silly is that!? > > Sorry. Anyway, any help would sure be great. > > > > oops actually it's not a direct base class, it's a base class of the base > > class of the base class. But still. Inheritance is supposed to work, > > right? lol > > > > > > > > > > | |||
| | #4 | ||
| "Shavais" <shavais@hotmail.com> wrote in news:oRdfb.395819$2x.133866@rwcrnsc52.ops.asp.att. net: > BTW yep, I proved my fear is true: > > After calling BaseClassManipulator.BaseClassMemberChanger(pObjec t), > > if I look at ((BaseClass *)pObject)->m_nValue, > > I get the correct result. And the other members of ((BaseClass > *)pObject) look fine, too. But if I look at > > ((DerivedClass *)pObject)->m_nValue, > > I get an incorrect result! Even though pObject was created as a > DerivedClass object. Not only that, but after calling > BassClassMemberChanger(), other members of ((DerivedClass *)pObject) are > now screwed up too. Oh what fun. Well, so much for my grand plans. I'm not sure what you're doing here. Have a code fragment: ----------------------------- #include <iostream> using namespace std; class Base { public: Base() { m_int = 1; } ~Base() {} void ChangeMe() { m_int = 2; } void ShowMe() { cout << m_int << endl; } int m_int; }; class Derived : public Base { public: Derived() {} ~Derived() {} void DerivedShowMe() { cout << m_int << endl; } }; int main() { Derived* pDerived = new Derived; Base* pBase = dynamic_cast<Base*>( pDerived ); pDerived->ShowMe(); pDerived->DerivedShowMe(); pBase->ShowMe(); pDerived->ChangeMe(); pDerived->ShowMe(); pDerived->DerivedShowMe(); pBase->ShowMe(); return 0; } ----------------------------- I think the code fragment above does what you're trying to do, and outputs (for me) exactly what I'd expect: 1 1 1 2 2 2 Chris -- Chris Whitworth - Programmer at Strangelite "back when I was young, we had to travel back in time to put the tape in so the game would load before we died." --otama, ua2 | |||
| Featured Websites | ||||
|
![]() |
| Tags: base, change, class, derived, functions, members, question |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| The May Curvemeister class is starting | Mike Russell | Graphics in general | 0 | 06-12-2007 12:23 AM |
| 1 class = 1 file? | Mark Wiesemann | Pear | 8 | 05-20-2007 7:41 PM |
| Featured Websites | ||||
|