[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 18:58:01 CEST 2005


David,

Here is an example (excuse the formatting, because I
don't know how to change it from 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;
};

The pyste code:
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)

An example of the error:
>>> from PysteError import *
>>> c = C()
>>> d = D()
>>> c.SetBptr(d)
>>> c.Update(1)
>>> print c.GetData_a()
10
>>> print c.GetData_b()
-9.0
>>> class E(B):
... 	def Update(self,data,val):
... 		data.a = -8;
... 		data.b = -1;
... 		
>>> e = E()
>>> c.SetBptr(e)
>>> c.Update(1)
>>> print c.GetData_a()
10
>>> print c.GetData_b()
-9.0
>>> cc = C()
>>> cc.SetBptr(e)
>>> cc.Update(1)
>>> print cc.GetData_a()
6488163
>>> print cc.GetData_b()
6.95352557184e-308

Hope this is clearer now ;) 

Thanks for you help,
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