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

Nat Linden nat at lindenlab.com
Fri Jun 8 17:14:43 CEST 2012


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.


More information about the Cplusplus-sig mailing list