[C++-sig] problem chaning object state using a python class that inherits from c++ class

Alexis H. Rivera-Rios ahrivera at yahoo.com
Tue Aug 16 00:56:06 CEST 2005


Abraham,

The idea is that I have a class Entity that contains a
pointer to a strategy object that implements the 
Trajectory interface. The Trajectory interface
modifies the Entity's state in UpdateState().  The
entity class delegates the update of its state to the
Trajectory strategy.

The problem is that in python, when I implement the
Trajectory interface and pass the object to the Entity
class, the call to Trajectory.UpdateState doesn't seem
to work correctly.  It gets executed but the state is
not updated.  (I guess it has to do with the fact that
I'm passing the object as a reference).

However, if I call UpdateState directly from the
python object that implements Trajectory interface it
works fine. (ie. it changes the content of the object)

If this is still too much let me know and I'll try to
reproduce the error with simpler classes.  But here is
the code.

class Entity
{
        /// specify a trajectory strategy
        /// which is a shared pointer to a Trajectory
object specified below
        SetTrajectory(const TrajectoryPtr &T) {
trajectory = T; }
	/// delegates to the Trajectory strategy
	/// the updating the state
	virtual void UpdateState(double ellapsedTime=1)
	{
            t->UpdateState(state,ellapsedTime);
	}
private:
	EntityState state; ///< object to be modified
	TrajectoryPtr trajectory; /// this is a shared
pointer 
};

class Trajectory  /// interface that i want to inherit
in python
{	
public:
        /// note it takes the state as a reference
	virtual void UpdateState(EntityState& state, double
ellapsedTime) const  = 0;
};

I exposed the class using pyste and in python I'm
derived a class from it.

class HackedT(Trajectory):	
   def UpdateState(self, entitystate, ellapsedTime):
 	entitystate.position = gmtl.Point3d(-1,-1,-1)
 		

The problem is that when I do this in python:
target = Entity()
target.SetTrajectory(hackedT)
target.UpdateState()  # enventually calls the
HackedT's UpdateState, which modifies the state
print target.GetPosition()

I expect the result to be (-1,-1,-1) but instead its
coming out to be the original value (10,10,0).

However, if I use Trajectory objects created from C++
then the state is modified.

Also, if I call the function directly it modifies the
state.

s = EntityState() ## by default s.position = (0,0,0)
hackedT.UpdateState(s,1)
print s.position ## returns (-1,-1,-1)


Is this a bug in Boost Python or do I have to specify
some policy or something to make this work?  Let me
know if you need additional information.  

Again, any help will be greatly appreciated, (Hope
it's better summarized)
Alexis

Programming Tutorial:
In Python: To do this, do this
In Perl: To do this, do this or this or this or this...
In C: To do this, do this, but be careful
In C++: To do this, do this, but don't do this, be careful of this, watch out for this, and whatever you do, don't do this

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Cplusplus-sig mailing list