Skip to content

Instantly share code, notes, and snippets.

@iamleeg
Created March 24, 2012 00:23
Show Gist options
  • Save iamleeg/2176672 to your computer and use it in GitHub Desktop.
Save iamleeg/2176672 to your computer and use it in GitHub Desktop.
Using operator overloading with Objective-C. Erm, ++.
#include "objc_id.hpp"
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *s1 = @"hello";
NSString *s2 = [@"hell" stringByAppendingString: @"o"];
if ((objc_id(s1) == objc_id(s2))) {
NSLog(@"win");
}
else {
NSLog(@"fail");
}
}
return 0;
}
#include <objc/objc.h>
#import <Foundation/Foundation.h>
#ifndef __objc_H_
#define __objc_H_
class objc_id {
private:
id<NSObject> object;
public:
objc_id(id<NSObject> theObject) {
[theObject retain];
object = theObject;
};
bool operator==(const objc_id& other) {
return [object isEqual: other.object];
};
~objc_id() {
[object release];
};
};
#endif //__objc_H_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment