1

Possible Duplicate:
Operator overloading in C

If I have a struct:

typedef struct myStruct {
    ...
} myStruct;

myStruct myStructAdd(myStruct a, myStruct b);

I need something like this:

#define myStruct a + myStruct b myStructAdd(a, b)
// NOTE this code does NOT WORK. This is what the question is asking.

To make this syntax valid:

myStruct a;
myStruct b;
myStruct c = a + b;

Is there any way to use a #define to do this?

EDIT:

I'm not asking for alternatives to the + syntax. What I'm asking is if, and how, the preprocessor can be used to rewrite the plus syntax to standard C syntax on compile.

i.e. something like #define myStruct a + myStruct b myStructAdd(a, b) which turns myStructA + myStructB into myStructAdd(myStructA, myStructB) on compile.

Community
  • 1
  • 1
Greg
  • 9,068
  • 6
  • 49
  • 91
  • I don't know about Objective C but I am not aware of a way to do this in C99. Macros can do a lot of magic but can't think of a way to overload the '+' operator for only specific structs without using parentheses. Will be interesting to see if you get a surprising positive reply! – Lefteris Feb 04 '12 at 10:19
  • `#define a + b` would mean `replace "a" with "+ b"`. Don't think it can be reasonably done with preprcessor. – vines Feb 04 '12 at 10:20
  • [dup](http://stackoverflow.com/questions/3613980/can-i-overload-an-operator-in-objective-c) – dreamlax Feb 04 '12 at 10:22
  • No, it's not a duplicate. The question you linked to asks how to use operator overloading in Objective-C, while I am asking if it is possible to use the preprocessor in C to achieve a _similar_ effect to operator overloading. – Greg Feb 04 '12 at 10:42
  • 1
    It *is* a dup. Operator overloading is operator overloading. If it were possible, it would have been answered in the question I linked to. Your question is simply a subset of the other. It's already been said in that question that operator overloading is **NOT** possible, ergo, it's not possible using preprocessor macros (your proposal). – dreamlax Feb 04 '12 at 11:56

4 Answers4

2

There is no way for you to do that using the preprocessor. Also, as far as I known, there is no other feature that would provide this in objective C.

However, if you would use C++ (or objective-C++, which give you all features of both Objective C and C++) you could define an operator+, as follows:

struct myStruct
{
  myStruct operator+(myStruct const & other)
  {
    return ...;
  }
}
Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • In Objective-C++ you *do* get the features of C++ and Objective-C, but only so far in that none of the features conflict with each other. You can't magically invoke C++ methods with `@selector()`, and you can't overload operators for Objective-C objects. – dreamlax Feb 04 '12 at 10:40
  • But operator overloading is apparently __evil__ (http://stackoverflow.com/questions/3613980/can-i-overload-an-operator-in-objective-c). I'm asking about how to achieve a similar effect with the preprocessor. – Greg Feb 04 '12 at 10:44
  • @NightLeopard the result of simulating it via the preprocessor would be more evil than using C++'s first class support for it. C++ is the way to go, if this is what you really need. – justin Feb 04 '12 at 10:59
2

Operator overloading simply isn't a feature of C or Objective-C. C++ allows you to define arbitrary behaviour for operators and custom types. In Objective-C, if two objects can be added together, then usually there is a method for that:

Foo *result = [foo1 fooByAddingFoo:foo2];

Or, if the class is mutable:

Foo *foo1 = [Foo fooWithBar:bar];

[foo1 addFoo:foo2];

If operator overloading is a must-have feature, use C++ instead, or use Objective-C++ (but keep in mind that C++ classes and Objective-C objects are totally and fundamentally different).


Edit:

The C proprocessor is conceptually very simple, and it knows very, very little about C's syntax, and nothing at all about C's types. If you wanted to overload an operator using the preprocessor, then it would have to learn every type (including custom types) used in your code, and it would have to perform static type checking in order to determine which function to invoke, and this is something that is way out of the scope of the preprocessor.

It's an interesting idea, but it's simply not possible.

Community
  • 1
  • 1
dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • I know I can do all that, my question was whether I can use the preprocessor to rewrite the + sign to standard C syntax on compile. – Greg Feb 04 '12 at 10:36
  • @NightLeopard: And I said that it is simply not a feature of C. There is no way in standard C to change the behaviour of the addition operator. – dreamlax Feb 04 '12 at 10:42
  • I just thought that maybe the preprocessor is powerful enough to rewrite `a + b` into `add(a, b)`. – Greg Feb 04 '12 at 10:50
  • @NightLeopard try it: **"Error: Macro names must be identifiers"** – justin Feb 04 '12 at 11:01
1

The only way I know is to use Objective-C++. To do this, give your implementation file the extension "mm" and you're good to go.

Ken
  • 30,811
  • 34
  • 116
  • 155
1

If you limit your question to the preprocessor then the answer is that it is impossible due to the fact that to define a macro that takes in arguments you have to have a parentheses macro like

#define __DO_STH(par1,par2)

Operator overloading the way you think of it does not use parentheses so you can not create any such macros

The only way to do that would be to make a simple parser which would be reading your code and whenever it encountered the structs you need being added with a plus sign spit out C code that replaces that with the function, but why do that and not use C++ where it's natively supported?

Also unless you are asking for purely academic purposes, it is my honest opinion that operator overloading always does more bad than good and is better avoided.

Lefteris
  • 3,196
  • 5
  • 31
  • 52