Thank you very much for your answer.
I may add two things:
1.
I modified the Python derived class in order to be sure that myC is treated as
mutable within Python
class APy(A):
def __init__(self):
A.__init__(self)
n = 0
x = 0.
myC = C()
self.g(myC)
print("value after a 1st call within Python: " + repr(myC.getValue()))
def f(self, n):
n = 5
return n
def g(self, myC):
x = 7.0
myC.setValue(x)
print("value in Python: " + repr(myC.getValue()))
As a result, myC is modified in the constructor's call (print("value after a
1st call within Python: " + repr(myC.getValue())) send 7 as expected). Thus,
myC is clearly treated as mutable (I wasn't sure about it).
2.
As I said, I can't modify the original C++ code as I would like.
In the original code, instead of a double, the "C" object is originaly a
vector, and I verify the size of the vector.
The original code look like this:
C CVector;
this->dosomething(&CVector)
and, though CVector is modified within dosomething (which is a C++ function
originaly, but I have to use the python overloaded version that I wrote), it
will lose the modification while using the python overloaded function.
As my function h was clumsy written, I modified it as well:
C h(){//This function wont be overloaded in Python
C myC;
this->g(&myC);//We want myC to be modified, and the python overloaded version
of g to be used
cout << "Value back in C++: " << myC.getValue() << endl; //We want to verify if
myC has been modified back in C++
return myC;
}
when g is called by h, myC is modified inside APy.g, but back to C++ I still
have the strange-looking value.
Isn't C myC the initialisation of myC?
As it work fine for the full C++ code, and as the argument is treated as
mutable within Python, I don't really understand what's wrong.
Moreover, I should avoid to modify the original C++ code as much as possible.
Jean-Joseph
________________________________
De : Nat Linden <n...@lindenlab.com>
À : Development of Python/C++ integration <cplusplus-sig@python.org>
Envoyé le : Vendredi 8 juin 2012 17h14
Objet : Re: [C++-sig] Re : changing argument value in a C++/Python code
On Fri, Jun 8, 2012 at 8:46 AM, christophe jean-joseph
<jjchristo...@yahoo.fr> wrote:
> class C
> {
> public:
> double myC;
> C(){}
> ~C(){}
>
> void setValue(double val) {
> myC = val;
> }
>
> double getValue() {
> return myC;
> }
> };
> ...
>
> class A
> {
> public:
> ...
> C h(){ //This function wont be overloaded in Python
> C myC;
> this->g(&myC); //We want myC to be modified, and the python overloaded
> version of g to be used
> C calledC;
> double myCint;
> myCint = calledC.getValue();
> cout << "Value back in C++: " << myCint << endl; //We want to verify if myC
> has been modified back in C++
> myCint = myCint*2;
> calledC.setValue(myCint);
> return myC;
> }
> };
>
> When I run the code, ...
> cout << "Value back in C++: " <<
> myCint << endl print -9.25596e+061 while I would like the variable to be at
> 7.
At risk of sounding foolish, your output looks reasonable to me.
In A::h(), at the point when you execute that cout << etc., this is
all the data that seems to participate:
> C calledC;
> double myCint;
> myCint = calledC.getValue();
> cout << "Value back in C++: " << myCint << endl;
You instantiate a brand-new 'C' object calledC. Its constructor
doesn't initialize C::myC, therefore it's uninitialized. You then call
calledC.getValue(), which returns that uninitialized double. myCint is
therefore a strange-looking value.
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig