• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Synthesized ivar for std::tr1::shared_ptr<MyClass>?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Synthesized ivar for std::tr1::shared_ptr<MyClass>?


  • Subject: Re: Synthesized ivar for std::tr1::shared_ptr<MyClass>?
  • From: Sherm Pendley <email@hidden>
  • Date: Thu, 6 May 2010 07:28:15 -0400

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

  • Follow-Ups:
    • Re: Synthesized ivar for std::tr1::shared_ptr<MyClass>?
      • From: Barry Wark <email@hidden>
References: 
 >Synthesized ivar for std::tr1::shared_ptr<MyClass>? (From: Barry Wark <email@hidden>)

  • Prev by Date: Re: Avoid drawing in half pixel
  • Next by Date: Re: Utility/Floating window with titlebar on left side
  • Previous by thread: Re: Synthesized ivar for std::tr1::shared_ptr<MyClass>?
  • Next by thread: Re: Synthesized ivar for std::tr1::shared_ptr<MyClass>?
  • Index(es):
    • Date
    • Thread