Please, write example about operator=

Asked by steep8

I'm not found how to deal with "operator=", please write here example.

Question information

Language:
English Edit question
Status:
Solved
For:
PyBindGen Edit question
Assignee:
No assignee Edit question
Solved by:
Gustavo Carneiro
Solved:
Last query:
Last reply:
Revision history for this message
Gustavo Carneiro (gjc) said :
#1

PyBindGen doesn't support operator=, nor does it make sense.

Do you mean operator== ? operator== is used transparently, no need to do anything...

Revision history for this message
steep8 (steep8) said :
#2

I mean Test& operator=(const Test& right),
why it nor does make sense?
test = Test()
test2 = Test()
test2 = test work well without operator= ?

Revision history for this message
Best Gustavo Carneiro (gjc) said :
#3

The issue is that C/C++ and Python differ completely in this respect.

In Python, test2 and test are simply pointers, test2 = test simply makes test2 point to the same object that test points to.

In C++, "test2 = test" makes test2 a copy of the object contained in test.

PyBindGen has the CppClass.add_copy_constructor() method to tell it to generate code to support copy-constructor. This means that the class will have a constructor that takes another class as parameter, so you can write "test2 = Test(test1)", which creates a copy. In addition, this triggers a __copy__ method to be generated, allowing you to use the copy module: from copy import copy; test2 = copy(test1).

I'm not sure if operator= has any effect in pybindgen, to be honest. If you use gccxml scanning, pybindgen calls the type_traits.has_copy_constructor() function, which may or not take operator= into account, I don't know.

Revision history for this message
steep8 (steep8) said :
#4

Thanks Gustavo Carneiro, that solved my question.