[C++-sig] How To Determine Return Policy???

chuzo okuda okuda1 at llnl.gov
Mon Jun 3 20:29:53 CEST 2002


Sorry, I forgot to add one more question.
Is there a way to write BPL code that do not have to add "Tag*" to the
argument for "static std::string str2()" in the source? I do not want to
alter original source codes many people wrote.
Thank you...
Chuzo


chuzo okuda wrote:
> 
> <snip>
> 
> And now I got another problem. At the end is a simple source code that
> works ok... str1() is working ok, but if static is added, I get
> TypeError message *UNLESS* you use str2(Tag*) instead of str2() and
> similarly for str3 which issue TypeError. Could you please educate me
> why "Tag*" is needed here, even though there is no need if you run in
> c++ code... (I found the example code in m1.cpp struct B which uses
> name(B*), and I followed this example and it worked... I dont understand
> what is going on, since it seems to me that I have to do trial and error
> differently many times until it succeeds...).
> 
> Thank you
> Chuzo
> 
> >>> from Test1a import *
> >>> t=Tag()
> >>> t.str1()
> 'string return successful'
> >>> t.str2()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: bad argument type for built-in operation
> >>> t.str3()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: bad argument type for built-in operation
> 
> -------------- source ----------------
> #include <boost/python/module.hpp>
> #include <boost/python/class.hpp>
> #include <boost/python/return_internal_reference.hpp>
> #include <boost/python/copy_const_reference.hpp>
> #include <boost/python/copy_non_const_reference.hpp>
> #include <boost/python/return_value_policy.hpp>
> #include <string>
> 
> class Tag {
> public:
>    Tag():mName("") {}
>    ~Tag() {}
>    std::string str1() {return "string return successful";}
>    static std::string str2(Tag*) {return "problem here...";}
>    static char const* str3(Tag*) {return "work???";}
> private:
>    std::string mName;
> };
> 
> BOOST_PYTHON_MODULE_INIT(Test1a)
> {
>    using namespace boost::python;
> 
>    class_<Tag> mod("Tag");
>    mod
>       .def_init()
>       .def("str1", &Tag::str1)
>       .def("str2", &Tag::str2)
>       .def("str3", &Tag::str3)
>       ;
> 
>    module("Test1a").add(mod);
> -------------------------------------------
> 
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig





More information about the Cplusplus-sig mailing list