C++ virtual method calls.

Liu Smith <[email protected]> Sun, 19 May 2024 16:33:38 +0800
Newsgroups gmane.comp.programming.swig
Message-ID <CA+gQ2mQBNEkM4Eo+1WE_iyUqyMCqu3ubs-mPq7FdDR94C107Vg@mail.gmail.com>
--===============2513741541239377896==
Content-Type: multipart/alternative; boundary="000000000000f6fb1d0618ca7944"

--000000000000f6fb1d0618ca7944
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hello,

I'm new with swig and I try to portage a C++ framework  to Python.
I'm reading the documentation for C++ class structures, but I encountered
some problems with C++ virtual method calls.
I used setattr in python to set an attribute, but after calling the C++
virtual method to python,
the attribute disappeared. Why is this happening? How can I prevent this
attribute from disappearing?
eg:


%feature("director") Bike;

class Wheel:
public:
   Wheel();
  virtual ~Wheel();
};

class Bike {
public:
  Bike();
  virtual ~Bike();
  void  setWheel(Wheel* w)
  {
m_wheel =3D w;
  };
  void printInfo()
  {
printWheel(m_wheel);
  }
  virtual void printWheel(Wheel* w) =3D 0;
private:
  Wheel* m_wheel;
};



then at the Python side=EF=BC=9A
import mymodule
class MyBike(mymodule.Bike):
    def printWheel(self, wheel):
    print(wheel.name)

wheel =3D Wheel()
setattr(wheel, "name", "myWheel")
bike =3D MyBike()
bike.setWheel(wheel)
bike.printInfo():


At this time, the error is as follows=EF=BC=9A
print(wheel.name)
AttributeError: 'Wheel' object has no attribute 'name

Thanks a lot

Smith

--000000000000f6fb1d0618ca7944
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><div>Hello,</div><div><br></div><div>I&#3=
9;m new with swig and I try to portage a C++ framework=C2=A0 to Python.</di=
v><div>I&#39;m reading the documentation for C++ class structures, but I en=
countered some problems with C++ virtual method calls.</div><div>I used set=
attr in python to set an attribute, but after calling the C++ virtual metho=
d to python,=C2=A0</div><div>the attribute disappeared. Why is this happeni=
ng? How can I prevent this attribute from disappearing?</div><div>eg:</div>=
<div><br></div><div><br></div><div>%feature(&quot;director&quot;) Bike;</di=
v><div><br></div><div>class Wheel:=C2=A0</div><div>public:</div><div>=C2=A0=
 =C2=A0Wheel();</div><div>=C2=A0 virtual ~Wheel();</div><div>};</div><div><=
br></div><div>class Bike {</div><div>public:</div><div>=C2=A0 Bike();</div>=
<div>=C2=A0 virtual ~Bike();</div><div>=C2=A0 void=C2=A0 setWheel(Wheel* w)=
</div><div>=C2=A0 {</div><div><span style=3D"white-space:normal"><span styl=
e=3D"white-space:pre">	</span>m_wheel =3D w;</span></div><div>=C2=A0 };</di=
v><div>=C2=A0 void printInfo()</div><div>=C2=A0 {</div><div><span style=3D"=
white-space:normal"><span style=3D"white-space:pre">	</span>printWheel(m_wh=
eel);</span></div><div>=C2=A0 }</div><div>=C2=A0 virtual void printWheel(Wh=
eel* w) =3D 0;</div><div>private:</div><div>=C2=A0 Wheel* m_wheel;</div><di=
v>};</div><div><br></div><div><br></div><div><br></div><div>then at the Pyt=
hon side=EF=BC=9A</div><div>import mymodule</div><div>class MyBike(mymodule=
.Bike):</div><div>=C2=A0 =C2=A0 def printWheel(self, wheel):</div><div><spa=
n style=3D"white-space:normal"><span style=3D"white-space:pre">	</span>=C2=
=A0 =C2=A0 print(<a href=3D"http://wheel.name">wheel.name</a>)</span></div>=
<div>=C2=A0 =C2=A0=C2=A0</div><div><span style=3D"white-space:normal">	</sp=
an></div><div>wheel =3D Wheel()</div><div>setattr(wheel, &quot;name&quot;, =
&quot;myWheel&quot;)</div><div>bike =3D MyBike()</div><div>bike.setWheel(wh=
eel)</div><div>bike.printInfo():</div><div><br></div><div><br></div><div>At=
 this time, the error is as follows=EF=BC=9A</div><div>print(<a href=3D"htt=
p://wheel.name">wheel.name</a>)</div><div>AttributeError: &#39;Wheel&#39; o=
bject has no attribute &#39;name</div><div><br></div><div>Thanks a lot</div=
><div><br></div><div>Smith</div></div></div>

--000000000000f6fb1d0618ca7944--


--===============2513741541239377896==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============2513741541239377896==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user

--===============2513741541239377896==--