56

Is it possible to override operator use in Objective-C?

For example

myClassInstance + myClassInstance

calls a custom function to add the two.

7 Answers 7

90

Operator overloading is not a feature of Objective-C. If two instances of your classes can be added together, provide a method and allow them to be added using that method:

Thing *result = [thingOne thingByAddingThing:thingTwo];

Or, if your class is mutable:

[thingOne addThing:thingTwo];
1
16

No, you can't do this in Objective-C.

1
  • any reason for that?
    – Raja Saad
    Jan 25, 2019 at 13:26
8

You can do this now in Swift, a successor to objC. And since Objective-C and Swift are made to work together This could be interesting for you.

2
  • 2
    Have a look at the Swift page on Apple's developer website. It says: Swift is a successor to the C and Objective-C languages. Aug 7, 2014 at 10:59
  • 2
    Ok. Could you post a concrete example? As I understand swift code may be included into Objective C one. Aug 11, 2015 at 14:08
3

You may want to support subscripting for your object. Subscripting is not operator overloading, but it can be handy for a collection object. NSArray and NSDictionary both support subscripting. For example:

NSMutableArray *a = [NSMutableArray new]; a[0] = @"Hello";

The way to support index subscripting is to implement the following:

-(id)objectAtIndexedSubscript:(NSUInteger)idx; -(void)setObject:(id)newObject atIndexedSubscript:(NSUInteger)idx];

1
  • 1
    Actually, [] is considered an operator in both C and C++, so in a sense, objectAtIndexedSubscript: is a way to overload an operator.
    – dreamlax
    Jul 1, 2015 at 7:04
2

I know this is an old question but I just wanted to leave this answer here for anybody in the future that might want to know if this is a possibility.

The answer is YES!

You'll have to use a variant of Objective-C called Objective-C++. As an example, say you created a new Objective-C command-line tool project. In order to allow C++ functionality, you'll need to rename "main.m" to "main.mm". Afterwards, you can mix C++ code in with your Objective-C code in the same file. There are some limitations, but I've tested operator overloading and it seems to work perfectly fine with Objective-C objects as far as I can tell. I've included sample source code to give you an idea of how to do it:

//main.mm
#import <Foundation/Foundation.h>
#include <iostream>
#include <string>

std::ostream &operator<<(std::ostream &os, NSString *s) {
    os << [s UTF8String];
    return os;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        NSString *str = @"I'm an NSString!";
        std::cout << str << std::endl;

    }
    return 0;
}

Here's my output after building and running this code:

I'm an NSString!
Program ended with exit code: 0

Hopefully this will be of help to somebody!

1
  • 1
    When I see Objective C++, I only wonder how the compiler does its job! C, Objective C and C++ altogether!
    – tf3
    Aug 26, 2020 at 1:02
0

No, Objective-C does not support operator overloading.

-28

First, operator overloading is evil. Second, C doesn't have operator overloading, and Objective-C is a proper superset of C, which only adds a handful of keywords and a messaging syntax.

That being said, if you're using Apple's development environment, you can use Objective-C++ instead of Objective-C, which gives you access to all of C++'s mistakes and misfeatures, including operator overloading. The simplest way to use Objective-C++ is just to change the extension on your implementation files from ".m" to ".mm"

18
  • 24
    I don't think it's fair to categorically say it's evil. It doesn't generally seem to pose a big problem in Smalltalk, Ruby, Python or Haskell.
    – Chuck
    Sep 1, 2010 at 0:25
  • 8
    If ever you need to recreate basic datatypes. (And I do) loosing operator overloading is crippling A+B*C-D becomes A.add(B.times(C)).Minus(C)) Nov 19, 2011 at 14:09
  • 5
    You've inadvertently given an example of why overloading is evil. What does it even mean to multiply a bezier curve? Dec 13, 2011 at 17:58
  • 22
    So let me understand: if I have Vector type (for each game programming language) which I have written, or similar things, you prefer to write myVector.Add(theOtherVector).Cross(somethingElse) instead of (myVector + theOtherVector) * somethingElse? Sorry, btu this answer is completely subjective and definitely not explained. Apr 19, 2012 at 1:27
  • 7
    "Operator overloading = evil".. Ignorance at its best.
    – Chris
    May 19, 2013 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.