|
Boost Users : |
Subject: [Boost-users] ´ð¸´: Boost-users Digest, Vol 2718, Issue 3
From: ÇÇ־ǿ (qiaozhiqiang_at_[hidden])
Date: 2011-05-10 21:20:41
typedef boost::shared_ptr<A> APtr;
void* callbk1 (void* o) {
APtr a = *static_cast<APtr*>(o); // or APtr& a = *static_cast<APtr*>(o)
a->hello();
}
void* callbk1 (void* o) {
A* a = static_cast<A*>(o);
a->hello();
}
boost::shared_ptr<A> sPtr(rawPtr);
if before a->hello(), the *rawPtr is not destroy(sPtr may be out of scope, but at least one boost::shared_ptr<A> holds it). Use:
callbk1(&sPtr); or
callbk2(sPtr.get());
If sPtr will out of the scope and no other boost::shared_ptr<A> holds *rawPtr, you should new a boost::shared_ptr<A>:
boost::shared_ptr<A>*p = new boost::shared_ptr<A> (sPtr); // keep A life.
// pass p to callback
callbk3(p); // may be invoke in other thread
void callbk3(void* o)
{
// delete the p = new APtr(sPtr) when return if we only call callbk3 once.
boost::scoped_ptr<Aptr> pp(static_cast<APtr*>(o));
APtr& a = *pp; // at least we have this one boost::shared_ptr<A>
a->hello();
}
Best
Regards!
Qiao Zhi Qiang
>
> ------------------------------
>
> Message: 6
> Date: Tue, 10 May 2011 14:24:26 +0530 (IST)
> From: Mupparthy Ravindranath <rv_nath_at_[hidden]>
> To: "boost-users_at_[hidden]" <boost-users_at_[hidden]>
> Subject: Re: [Boost-users] How to cast a boost::shared_ptr to void*
> Message-ID: <443447.3562.qm_at_[hidden]>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Sorry for the spam.? I got the answer, how to do it. Ignore my previous mail.
>
> [quote]
> #include <boost/shared_ptr.hpp>
> #include <iostream>
> using namespace std;
>
> class A {
> ??? public:
> ??? void hello () { cout << "Hello, friends...!" << endl; }
> };
>
> typedef boost::shared_ptr<A> APtr;
>
> void* callbk (void* o) {
> ??? APtr a = *static_cast<APtr*>(o);
> ??? a->hello();
> }
>
> int main() {
> A* rawPtr = new A;
> boost::shared_ptr<A>sPtr(rawPtr);
> callbk((void*)&sPtr);
> return 0;
> }
> [/quote]
>
>
> warm regars,
> RV
>
>
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