[C++-sig] Re: abstract templates and pyste question.

Hanz Meizer steam at nurfuerspam.de
Thu Feb 19 10:48:17 CET 2004


Hi,

Example:
--------------------------------------------------
class Base {
         public:
                 Base() {}
                 virtual ~Base() {}

                 virtual int getSize() = 0;

         private:
                 Base(const Base&) {}
};

class Derived: public Base {
         public:
                 Derived() {}
		virtual ~Derived() {}

                 virtual int getSizeD() = 0;
};

class Derived2: public Derived
{
   public:
     Derived2() {}
     virtual ~Derived2() {}

     virtual int getSize() { return 0; }
     virtual int getSizeD() { return 0; }
};

--------------------------------------------------

Output (with your earlier patch):
--------------------------------------------------
// Boost Includes 
==============================================================
#include <boost/python.hpp>
#include <boost/cstdint.hpp>

// Includes 
====================================================================
#include <Test.h>

// Using 
=======================================================================
using namespace boost::python;

// Declarations 
================================================================
namespace  {

struct Base_Wrapper: Base
{
     Base_Wrapper(PyObject* self_):
         Base(), self(self_) {}

     int getSize() {
         return call_method< int >(self, "getSize");
     }

     PyObject* self;
};

struct Derived_Wrapper: Derived
{
     Derived_Wrapper(PyObject* self_, const Derived& p0):
         Derived(p0), self(self_) {}

     Derived_Wrapper(PyObject* self_):
         Derived(), self(self_) {}

     int getSizeD() {
         return call_method< int >(self, "getSizeD");
     }

     int getSize() {
         return call_method< int >(self, "getSize");
     }

     PyObject* self;
};

struct Derived2_Wrapper: Derived2
{
     Derived2_Wrapper(PyObject* self_, const Derived2& p0):
         Derived2(p0), self(self_) {}

     Derived2_Wrapper(PyObject* self_):
         Derived2(), self(self_) {}

     int getSize() {
         return call_method< int >(self, "getSize");
     }

     int default_getSize() {
         return Derived2::getSize();
     }

     int getSizeD() {
         return call_method< int >(self, "getSizeD");
     }

     int default_getSizeD() {
         return Derived2::getSizeD();
     }

     PyObject* self;
};


}// namespace


// Module 
======================================================================
BOOST_PYTHON_MODULE(pytest)
{
     class_< Base, boost::noncopyable, Base_Wrapper >("Base", no_init)
         .def("getSize", pure_virtual(&Base::getSize))
     ;

     class_< Derived, bases< Base > , boost::noncopyable, 
Derived_Wrapper >("Derived", no_init)
         .def("getSizeD", pure_virtual(&Derived::getSizeD))
         .def("getSize", pure_virtual(&Base::getSize))
     ;

     class_< Derived2, bases< Derived > , Derived2_Wrapper >("Derived2", 
init<  >())
         .def(init< const Derived2& >())
         .def("getSize", (int (Derived2::*)() )&Derived2::getSize, (int 
(Derived2_Wrapper::*)())&Derived2_Wrapper::default_getSize)
         .def("getSizeD", (int (Derived2::*)() )&Derived2::getSizeD, 
(int (Derived2_Wrapper::*)())&Derived2_Wrapper::default_getSizeD)
     ;

}
--------------------------------------------------

Compiler:
--------------------------------------------------
../pyste.sh --module=pytest pytest.pyste
Module pytest generated
0.04 seconds
g++ -g -s -I. -Wall -ftemplate-depth-100 -DBOOST_PYTHON_DYNAMIC_LIB 
-fno-inline -fPIC -Iwork/pyste/boost.current -I/usr/include/python2.2 
pytest.cpp -c -o pytest.o
Test.h: In copy constructor `Derived::Derived(const Derived&)':
Test.h:9: error: `Base::Base(const Base&)' is private
pytest.cpp:30: error: within this context
make: *** [pytest.o] Error 1
--------------------------------------------------

H.





More information about the Cplusplus-sig mailing list