[C++-sig] wrapping operator=

Hans Roessler hansroessler at yahoo.de
Mon May 25 16:08:47 CEST 2009

I want to wrap with boost.python a C++ class "DataCube" which has overloaded the operator=.

The  constructor DataCube::DataCube(double x) allocates huge amounts of memory and fills it with x's.
The  DataCube::operator=(double x) just overwrites the already allocated memory with x's.

Now in C++ these commands first allocate memory, which is filled with 0.'s, and then overwrite the memory with 42.'s:
>DataCube data(0.)

In Python these commands first build the DataCube as desired, but then set data=42. (now data is a float), where the reference to the DataCube is lost:

I have circumvented this by replacing the second line with
> data.assign(42.)
with a function assign which is defined appropriately, but I would prefer to use the same assignment as in C++.

Finally the question:
Can I define the DataCube class in Python, so that the data variable above will behave as in C++, when I write "data=42." ?
In other words, is there any possibility in Python that "x=y" does NOT make x a reference to y?

Thank you


More information about the Cplusplus-sig mailing list