views:

872

answers:

3

I am currently using xcode for some c++ development & I need to generate getters & setters.

The only way I know is generating getters & setters in Objective C style

something like this - (string)name; - (void)setName:(string)value;

I dont want this; I want c++ style generation with implementation & declaration for use in the header files.

Any idea...?

+3  A: 

Objective C != C++.

ObjectiveC gives you auto-implementation using the @property and @synthesize keywords (I am currently leaning ObjectiveC myself, just got a Mac!). C++ has nothing like that, so you simply need to write the functions yourself.

Foo.h

inline int GetBar( ) { return b; }
inline void SetBar( int b ) { _b = b; }

or

Foo.h

int GetBar( );
void SetBar( int b );

Foo.cpp

#include "Foo.h"

int Foo::GetBar( ) { return _b; }
void Foo::SetBar( int b ) { _b = b; }
Ed Swangren
Asad Khan
XCode is not generating anything in ObjectiveC, that is a language feature, XCode is the IDE.
Ed Swangren
A: 

something.h:

@interface something : NSObject
{
   NSString *_sName;  //local
}

@property (nonatomic, retain) NSString *sName;

@end

something.m :

#import "something.h"
@implementation something

@synthesize sName=_sName; //this does the set/get

-(id)init
{
...
self.sName = [[NSString alloc] init];
...
}

...


-(void)dealloc
{
   [self.sName release]; 
}
@end
inked
The OP was asking for a C++ implementation.
Ed Swangren
This code also leaks - the [[NSString alloc] init] is never released.
phooze
+2  A: 

It sounds like you're just looking for a way to reduce the hassle of writing getters/setters (i.e. property/synthesize statements) all the time right?

There's a free macro you can use in XCode to even generate the @property and @synthesize statements automatically after highlighting a member variable that I find really helpful :)

If you're looking for a more robust tool, there's another paid tool called Accessorizer that you might want to check out.

Ray Wenderlich