[C++-sig] Having problems with returned object.

Fabzter faboster at gmail.com
Tue Mar 16 04:25:41 CET 2010


Hi everybody, I'll try to describe my problem:

I have this c++ class (simplyfied):

-------------------------------------------------------------------------------------------
class MyClass
{
public:
    MyClass()
    {
        this->setPos(0, 0);
    }
    MyClass(const MyClass& orig)
    {
        this->setPos(orig.pos);
    }

    void setPos(int x, int y)
    {
        this->pos.clear();
        this->pos.push_back(x);
        this->pos.push_back(y);
    }
    void setPos(const std::vector<int>& p)
    {
        this->pos = p;
    }
    const std::vector<int>& getPos() const
    {
        return this->pos;
    }
private:
    std::vector<int> pos;
};
-------------------------------------------------------------------------------------------

And then, it's wrapped like this:

-------------------------------------------------------------------------------------------
typedef void (MyClass::*setPos_with_int)(int x, int y);
typedef void (MyClass::*setPos_with_vect)(const std::vector<int>& pos);

void export_jugada()
{
    class_<MyClass>("MyClass")
        .def("setPos", setPosicion_with_int(&MyClass::setPos) )
        .def("setPos", setPosicion_with_vect(&MyClass::setPos) )

        .def("getPosicion", &MyClass::getPosicion,
                            return_value_policy<reference_existing_object>())
    ;
}
-------------------------------------------------------------------------------------------

Now, I have a little script that does something like:

-------------------------------------------------------------------------------------------
...
def get()
    m = MyClass()
    m.setPos(4, 9)
    return m
...
-------------------------------------------------------------------------------------------

I have debugged (as much as my knowledge lets me!!!) to this point and
everything seem fine. "get()" gets properly called, and when it calls
MyClass::setPos(int, int), it seems to do everything fine (filling up
the vector). Well, now, in the main program, after reading the script,
I do something like this:

-------------------------------------------------------------------------------------------
...extracting get() from script...
MyClass m ( getObj() );
-------------------------------------------------------------------------------------------

The problem here is that m ends up having pos = {4, 0}. The second
element from the vector is ALWAYS == 0. While debugging I discovered
that when the returned object is being copied (via copy constructor),
the "pos" member of the origin (the object that came from python) has,
well, it's second element == 0. The same happens if I do something
like:

-------------------------------------------------------------------------------------------
...extracting get() from script...
MyClass m =   getObj() ;
-------------------------------------------------------------------------------------------

Please, help me find my mistakes, I've already spent hours trying to
solve this :(


More information about the Cplusplus-sig mailing list