Re: Synthesized ivar for std::tr1::shared_ptr<MyClass>?
Re: Synthesized ivar for std::tr1::shared_ptr<MyClass>?
- Subject: Re: Synthesized ivar for std::tr1::shared_ptr<MyClass>?
- From: Barry Wark <email@hidden>
- Date: Thu, 6 May 2010 16:18:45 -0700
Thank you all for the suggestions. Using manual dynamic memory
allocation somewhat defeats the purpose of using smart pointers, but
it looks like that's the only option for now. For reference, I've
filed this as rdar://7953539, but would be happy to be shown the error
of my ways.
Cheers,
Barry
On Thu, May 6, 2010 at 4:28 AM, Sherm Pendley <email@hidden> wrote:
> On Thu, May 6, 2010 at 2:19 AM, Barry Wark <email@hidden> wrote:
>> I'm no C++ guru, so I'm hoping someone can help me by explaining why
>> the following gives a compiler error:
>
> I'm no guru either, but my guess would be that the interaction between
> synthesized ivars and C++ templates is rocky territory. I avoid going
> down that path, declare the ivar as a pointer (with an #ifdef to
> declare it as a void* for ObjC client code) and create the object with
> new in +init. Here's a snippet from a random number generator class I
> wrote, that uses Boost's random module "under the hood":
>
> In SPRandom.h:
>
> #ifdef __cplusplus
> #import <boost/random.hpp>
> #endif
>
> typedef struct {
> int min;
> int max;
> } SPRandomRange;
>
> @interface SPRandom : NSObject {
> #ifdef __cplusplus
> boost::uniform_int<> *distribution;
> boost::variate_generator<boost::lagged_fibonacci607&,
> boost::uniform_int<> > *generator;
> #else
> void *distribution;
> void *generator;
> #endif
> }
> ...
> @end
>
> In SPRandom.m:
>
> static boost::lagged_fibonacci607 rng;
>
> @implementation SPRandom
>
> - (id) initWithRange:(SPRandomRange)range {
> self = [super init];
> if (self) {
> distribution = new boost::uniform_int<>(range.min,range.max);
> generator = new
> boost::variate_generator<boost::lagged_fibonacci607&,
> boost::uniform_int<> >(rng, *distribution);
> }
> return self;
> }
>
> - (void) dealloc {
> delete generator;
> delete distribution;
>
> [super dealloc];
> }
>
> @end
>
> sherm--
>
> --
> Cocoa programming in Perl:
> http://www.camelbones.org
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden