首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于boost_python库的使用中操作符重载的有关问题

2012-02-04 
关于boost_python库的使用中操作符重载的问题classCAA{public:CAA(){aa10}intgetValue(){returnaa}void

关于boost_python库的使用中操作符重载的问题
class   CAA
{
public:
        CAA(){   aa   =   10;   }
        int   getValue()   {   return   aa;   }
        void   setValue(   int   k   )   {   aa   =   k;   }
        CAA&   operator   []   (int   k   )   {   return   *this;   }
        CAA&   operator   +     (int   k   )   {   return   *this;   }
private:
        int   aa;
};

使用boost_python

BOOST_PYTHON_MODULE(   module_name   )
        class_ <CAA> (   "CAA "   )
                .def( "setValue "   ,   &CAA::setValue   )
                .def( "getValue "   ,   &CAA::getValue   )
                .def(   self   +   int()   )
                .def(   self   []   int()   )
        ;

两个问题:
1.[]符号重载这样写不正确,编译不通过
2.+符号重载的写法虽然可以编译通过,但是返回值不是期望的类型
    以下是python脚本
    > > >   from   module_name   import   *
    > > >   x   =   CAA()
    > > >   y   =   x   +   10    
    > > >   x.getValue()
    10
    > > >   x.setValue(20)
    > > >   y.getValue()
    10
    > > >   x.getValue()
    20
  我希望y.getValue()输出是20,就是希望y是x的一个引用,但实际上y.getValue()属出时10,请高手指点,谢谢

[解决办法]
CAA本身没有错,你还是认真看boost::python的文档,估计是 class_没用对。
[解决办法]
没做过 不懂
[解决办法]
你的类CAA里面的重载操作符没有写错啊
虽然CAA& operator [](int k){return *this;}显得比较没用。
还是在boost::python里面找找问题吧。

热点排行