Computer Webmaster Gaming Console Graphics Forum

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.

MK PitStop Main Earn $25 Earn Money Posting Extras Members Blogs Image Hosting User Pages
Go Back   Computer Webmaster Gaming Console Graphics Forum > Computer Forums > Software Programming
Register FAQ/Rules Become A V.I.P. Member Search Today's Posts Mark Forums Read

Software Programming Software programming talk, ask questions about computer software programming or help others

Google
Closed Thread
 
LinkBack Thread Tools Display Modes
Old 06-12-2007, 10:23 PM   #1
Shavais
 
Shavais's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default C++ question: How to change base class members in functions that don't know about the derived class

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




 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Advertisements
Old 06-12-2007, 10:23 PM   #2
Shavais
 
Shavais's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default C++ question: How to change base class members in functions that don't know about the derived class

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
>
>
>
>



 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 06-12-2007, 10:23 PM   #3
Shavais
 
Shavais's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default C++ question: How to change base class members in functions that don't know about the derived class

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
> >
> >
> >
> >

>
>



 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 06-12-2007, 10:23 PM   #4
Chris Whitworth
 
Chris Whitworth's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default C++ question: How to change base class members in functions that don't know about the derived class

"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

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Featured Websites
Free Space
Free Space
Free Space Free Space
Closed Thread
Tags: , , , , , ,




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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




All times are GMT +1. The time now is 12:36 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.0.0
Cheap Computers
MK PitStop Copyright 2005 - 2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98