Programas C/C++ utilizando apontadores opacos

Autor: rubens
Data: 30/04/2009

Exemplo

Módulo C++

01. struct RCM {
02.     int i;
03.     int j;
04. };
05. 
06. RCM *new_rcm(int a, int b)
07. {
08.     RCM *rcm = new RCM;
09.     rcm->i = a + 10;
10.     rcm->j = b + 100;
11.     return rcm;
12. }
13. void get_rcm(RCM *rcm, int *i, int *j)
14. {
15.     *i = rcm->i;
16.     *j = rcm->j;
17. }
18. void del_rcm(RCM *rcm)
19. {
20.     delete rcm;
21. }
OK [C/C++ extension is up-to-date]

Módulo Python

1. class RCM(object):
2.     def __init__(self, a, b):
3.         self._rcm = new_rcm(a, b)
4.     def __del__(self):
5.         del_rcm(self._rcm)
6.     def get(self):
7.         return get_rcm(self._rcm)

Teste

<opaque.RCM object at 0x2e65a50> _20740154867f0000_p_RCM
[35, 100]