Methods
Instance Methods
All public methods are wrapped and callable from Python. Overloaded methods are automatically dispatched based on argument types.
// Instance methods wrapping
#include <c2py/c2py.hpp>
namespace mylib {
class rectangle {
double width_, height_;
public:
rectangle(double w, double h) : width_(w), height_(h) {}
// Const method
double area() const { return width_ * height_; }
// Non-const method
void scale(double factor) {
width_ *= factor;
height_ *= factor;
}
// Overloaded methods
void resize(double factor) { scale(factor); }
void resize(double w, double h) {
width_ = w;
height_ = h;
}
};
} // namespace mylib
The Python usage:
from mymodule import Rectangle
r = Rectangle(10.0, 20.0)
print(r.area()) # 200.0
r.scale(2.0)
print(r.area()) # 800.0
Const Methods
Const methods are properly wrapped and can be called on const objects.
// Const methods
#include <c2py/c2py.hpp>
#include <string>
namespace mylib {
class book {
std::string title_;
std::string author_;
public:
book(std::string title, std::string author) : title_(std::move(title)), author_(std::move(author)) {}
// Const accessors
std::string const &title() const { return title_; }
std::string const &author() const { return author_; }
// Non-const mutator
void set_title(std::string title) { title_ = std::move(title); }
};
} // namespace mylib
Static Methods
Static methods are exposed as class methods in Python.
// Static methods
#include <c2py/c2py.hpp>
namespace mylib {
class math_utils {
public:
// Static methods
static int add(int a, int b) { return a + b; }
static int multiply(int a, int b) { return a * b; }
// Static factory method
static math_utils create() { return math_utils(); }
};
} // namespace mylib
The Python usage:
from mymodule import MathUtils
result = MathUtils.add(3, 5) # Call without instance
print(result) # 8