[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 20:51:54 CEST 2005


David,

Im sending the email again, since I don't remember if
I  have send it or not.  Please forgive the formatting
since I don't know how to control that with yahoo
mail.

struct Data
{
	int a;
	double b;
};

class B
{
public:
	virtual void Update(Data& a, int ellapsedTime) = 0;
	~B() {}
};

class D : public B
{
	virtual void Update(Data& a, int ellapsedTime)
	{
		a.a = 10;
		a.b = -9;
	}
};

#include <boost/shared_ptr.hpp>
class C
{
public:
	void SetBptr(boost::shared_ptr<B> newB)
	{
		Bptr = newB;
	}
	void Update(int ellapsedTime)
	{
		if (Bptr.get())
		{
			B *ptr = Bptr.get();
			ptr->Update(d,ellapsedTime);
		}
	}
	int GetData_a() const 
	{
		return d.a;
	}
	double GetData_b() const
	{
		return d.b;
	}
private:
	Data d;
	boost::shared_ptr<B> Bptr;
};

Here is the pyste script 

Class("Data","test.h")
B = Class("B","test.h")
use_shared_ptr(B)

Class("C","test.h")

D = Class("D","test.h")
final(D.Update)

Here is an example of the error:

(test 1) works as expected
>>> from PysteError import *
>>> c = C()
>>> d = D()
>>> c.SetBptr(d)
>>> c.Update(1)
>>> print c.GetData_a()
10
>>> print c.GetData_b()
-9.0

(test 2) fails when called from C
>>> class E(B):
... 	def Update(self,data,val):
... 		data.a = 999
... 		data.b = 100
... 		
>>> e = E()
>>> cc = C()
>>> cc.SetBptr(e)
>>> cc.Update(1)
>>> print cc.GetData_a()
6488163
>>> print cc.GetData_b()
6.95352557184e-308

(test 3) but it works when called directly
>>> data = Data()
>>> e.Update(data,1)
>>> print data.a
999
>>> print data.b
100.0
>>> 

Hope this illustrates the problem better,

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


		
____________________________________________________
Start your day with Yahoo! - make it your home page 
http://www.yahoo.com/r/hs 
 



More information about the Cplusplus-sig mailing list