| 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
Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.