Is there a way to return object reference

Asked by kun

Thanks for the answer of my previous question and the quick improvement to the code!

Now I am troubled by another question. I notice that there is way to wrap object reference
in function call parameter (CppClassRefParameter). But there is no such mechanism for
return value if the function returns an object reference (there is only CppClassPtrReturnValue).
Is it also sth that haven't got done but probably should be there?

Kun

Question information

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

It is technically difficult and dangerous to store C++ references in wrappers. So my advice is to use CppClassReturnValue for "Class&" actual return values. The end result will be that Python will create a wrapper containing a copy of the C++ object, not a reference, but otherwise things should just work the same.

If you really need a reference to a C++ object, I'm afraid PyBindGen does not support it and you should change the API to return a pointer instead. While I would not go as far as to say supporting real return references is impossible, I would say it is difficult and dangerous, and so I have no plans to support it. Just pretend it is returned by value and things should be OK.

Revision history for this message
Sures Kannan (kanaero) said :
#2

Hey,

I have the similar issue. I have multiple accessor methods that look like this

class Vehicle {

    Vector3d& pos() {return pos_;}
private:
    Vector3d pos_;
};

Is this possible ? I am asking this again since the question in this thread is a few years old. I am thrashing between Boost.Python and Pybindgen. Boost.Python seems to be happy handling the return of references.

I like pybinden way better than Boost Python. Using pointers would mean I have to deference every-time I use it. This is very inconvenient when writing equations. I cannot really swallow the Boost.Python compile time. It is way too slow.

By the way Gustavo, I wish to encourage your excellent work on PyBindGen. Most impressive.

regards
suresh.

Can you help with this problem?

Provide an answer of your own, or ask kun for more information if necessary.

To post a message you must log in.