[C++-sig] Exposing copy constructors with Boost::Python

Christopher Dawson dawson at physics.uq.edu.au
Thu Jul 22 06:42:20 CEST 2004

Apologies if this is a common problem with a common solution, but I haven't been able to find it anywhere.

I have a simple class which stupidly has a c-style array as a member:

class Test {
	int *a;

	Test(int N) {
		a = new int[N];

	Test(const Test& T) {
		a = new int[T.N];

	Test& operator=(const Test& T) {
		if (N != T.N) {
			delete[] a;
			a = new int[N];
			for (int i=0;i<N;i++)
				a[i] = T.a[i];
	~Test() {
		if (a) 
			delete[] a;

I have wrapped this with Boost::Python (1_31_0), and understood that the copy and assignment constructors would be wrapped automatically.A

However copying the object from Python just copies the pointer, not the array. I can explicitly expose the copy constructor with

	.def(init<const Test&>())

so  it's possible to do 
	a = Test(4)
	b = Test(a),

but I'd like to be able to to

	b = a

Have I missed something about exposing copy constructors?




More information about the Cplusplus-sig mailing list