Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-03-02 19:39:26


adharankar_at_[hidden] wrote:

> class A {
> public:
> int val;
> };
> typedef boost::shared_ptr<A> APtr;
>
> struct S {
> public:
> APtr aptr;
> };
>
>
> struct S *func(void) {
> A *a = new A;
> a->val = 12;
>
> APtr aptr(a);
>
> struct S *s = (struct S *)malloc(sizeof(struct S));

s->aptr is uninitialized here because you haven't invoked the constructor of
S yet. s now points to raw bytes, not to an object of type S. Use

    new( s ) S;

to construct an object in this storage, or better yet, just use

    S * s = new S;

> s->aptr = aptr;
> return s;
> }


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net