Skip to content

Instantly share code, notes, and snippets.

@atifkarim
Last active February 19, 2021 01:03
Show Gist options
  • Save atifkarim/2240afd009443ecd663f31113b7b31d4 to your computer and use it in GitHub Desktop.
Save atifkarim/2240afd009443ecd663f31113b7b31d4 to your computer and use it in GitHub Desktop.
class template binded with pybind
#include <pybind11/pybind11.h>
#include "class_template_inheritance.h"
namespace py = pybind11;
/**
* Base class
*/
template <typename T = int>
void class_template_base(py::module &m, const std::string& typestr1)
{
py::class_< Base<int>>(m, "Base")
.def(py::init<int>());
};
/**
* Derived class
*/
void class_template_derived(py::module &m)
{
py::class_<Der, Base<int>> Der (m, "Der");
Der.def(py::init<int,int>())
.def("show_der_val", &Der::show_der_val);
};
PYBIND11_MODULE(somecode, m)
{
class_template_base<int>(m, "int");
class_template_derived(m);
}
/*
<%
setup_pybind11(cfg)
%>
*/
#include <iostream>
using namespace std;
/**
* class template which will be inheritted
**/
template<typename T = int>
class Base
{
protected:
T base_Variable;
public:
Base(T x):base_Variable{x}{
std::cout<<"Base Class Constructor is called and Mem Var: "<<base_Variable<<std::endl;
}
virtual ~Base() {}
};
class Der : public Base<int>
{
public:
Der(int a, int b):Base<int>{b}, derived_Variable{a}{}
void show_der_val(){
std::cout<<"val of derived class private mem is: "<<derived_Variable<<std::endl;
};
virtual ~Der(){}
private:
int derived_Variable;
};
import somecode
class_temp_inherit_obj_base = somecode.Base(-878)
class_temp_inherit_obj_derived = somecode.Der(-985, 457)
class_temp_inherit_obj_derived.show_der_val()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment