Re: [C++-sig] problem chaning object state using a python class that inherits from c++ class
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
"Alexis H. Rivera-Rios" <ahrivera@yahoo.com> writes:
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.
THe formatting is okay, but... <snip code>
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)
Sorry, but I don't know Jack about pyste, so please show me the code it generates instead.
Here is an example of the error:
(test 1) works as expected
And please don't show me anything that works as expected. That's just a distraction from the problem.
(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
Please show me the result you were expecting, like I asked you to. I'm guessing it was 999 and 100.0 ??
Hope this illustrates the problem better,
Sorta. This example could be smaller, couldn't it? Do you really need two data members to show it? I really mean it: if you pare it down to the absolute minimum you'll probably find the problem yourself. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
Alexis H. Rivera-Rios -
David Abrahams