[C++-sig] Lifetime of Python object for a pointer argument
Min Xu
minxu at sci.ccny.cuny.edu
Sat Feb 16 17:40:50 CET 2002
On Sat, 2002-02-16 at 11:14, Achim Domma wrote:
> Hi,
>
> I'm also not an expert, so hopefully Dave will correct my if I'm wrong. As
> far as I know it's in general not an good idea to pass objekts as pointers
> in the current version of boost.python, so you should prefer passing
> parameters by value.
According to the document section on pointers and smart pointers, the
const ptr* as an argument is handled automatically.
An return value of type const ptr* does work in this code. See
>>> g=grid()
>>> a=geometry (g)
>>> b=a.getGrid ()
>>> del a
>>> b.a()
1.0 # Expected. b has a separate life from a as the copy constructor is
used in b=a.getGrid()
>
> > // C++ part
> > class grid
> > {
> > public:
> > grid() { ... }
> > ...
> > };
>
> Do you allocate dynamic memory in grid ? If yes, is the copy-constructor
> implemented ?
>
The grid class does not dynamic allocate memory and the copy constructor
is implemented.
The simplified class of grid is like this:
class grid
{
public:
grid() : _a(1) {...}
grid(double a, ...) : _a(a) {...}
grid(const grid& g) {...}
double a() const { return _a; }
private:
double _a;
};
> greetings
> Achim
Thanks.
>
>
>
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>
More information about the Cplusplus-sig
mailing list