Adding New Methods |
Suppose that you want to add a function to a class, turning it into a member function:
struct World
{
void set(std::string msg) { this->msg = msg; }
std::string msg;
};
std::string greet(World& w)
{
return w.msg;
}
Here, we want to make greet work as a member function of the class World. We do that using the add_method construct:
W = Class("World", "hello.h")
add_method(W, "greet")
Notice also that then you can rename it, set its policy, just like a regular member function:
rename(W.greet, 'Greet')
Now from Python:
>>> import hello
>>> w = hello.World()
>>> w.set('Ni')
>>> w.greet()
'Ni'
>>> print 'Oh no! The knights who say Ni!'
Oh no! The knights who say Ni!
Copyright © 2003 Bruno da Silva de Oliveira
Copyright © 2002-2003 Joel de Guzman
Distributed under
the Boost Software License, Version 1.0. (See accompanying file
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)