[C++-sig] Re : Re : changing argument value in a C++/Python code, about initialisation

christophe jean-joseph jjchristophe at yahoo.fr
Fri Jun 8 17:59:44 CEST 2012



I changed the first line of A::h to

C myC;
myC.setValue(0.);
this->g(&myC);

I don't know if it match your comment, but the result is myC read at 0 instead of a strange-value, which is still not 7.

CHRISTOPHE Jean-Joseph






________________________________
 De : Nat Linden <nat at lindenlab.com>
À : Development of Python/C++ integration <cplusplus-sig at 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
<jjchristophe at 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 at python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120608/3f19b9e7/attachment-0001.html>


More information about the Cplusplus-sig mailing list