Operator 'overloading' equivalent with #de

2019-08-02 19:38发布

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.

4条回答
We Are One
2楼-- · 2019-08-02 19:52

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-08-02 20:01

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 ...;
  }
}
查看更多
Emotional °昔
4楼-- · 2019-08-02 20:03

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.

查看更多
做个烂人
5楼-- · 2019-08-02 20:03

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.

查看更多
登录 后发表回答