Is it possible to do operator overloading or something similar (inline function?) in C? I know that c does not support class, but could I make an operator for a struct?
I cannot find anything about this online, because Google will ignore '+' so if I try to google this I only get C++ results.
No, you can't do that in C. Use C++ if you want to overload operators.
You can put function pointers inside a structure if you want a sort of C++ object-like behaviour.
No it is not possible.
By the way, you can remove C++ from google search results if you add -"C++" to your search query.
C++ introduced an important and interesting feature which is operator overloading.
So you will have to use it if you want to use this feature.
C does not support operator overloading or having functions inside structs. You will need to use C++ for those features.
C does not support operator overloading.
Related
There are no classes in C, but it is simple enough to create something similar by defining a struct that contains all the "class"'s properties, and representing its methods by functions that take pointers to said struct as their first arguments. I am in the process of documenting a header file of this form.
I'm trying to figure out how to refer to this sort of function without using object-oriented terminology like "method". I could just invent my own name for it and define it, but considering how common these sort of functions are, I assume there's already a standard way to refer to them. Am I correct? If so, how do I refer to them?
I would use both "function" and "method".
"Function" would refer to the implementation, since it is actually a function, not a method.
"Method" would refer to how the function is used, since you want to emulate OOP, and you even call it as-if it would be a method. Of course, you will not be able to use advanced OOP features of the "method", but you accepted that when you decided to use C instead of C++.
I would call them methods.
If Rust can call it methods, sure you can.
I know that C uses pass-by-value and we can emulate pass-by-reference with the help of pointers. But, for example, in order to calculate a simple mathematical expression, how do I implement pass-by-name (which is kind of lazy evaluation but not exactly) in C?
C is only pass-by-value. You can't pass by reference or name. With the pre-processor you can do various hacks but not in the C language.
Sometimes, people call passing a pointer "pass-by-reference" but this is not the case. The pointer is passed by value like anything else. C++ is a different story but you asked about C.
You might also be interested in this article discussion this at length
The parameter substitution used by function-like preprocessor macros is sometimes described as being "pass by name".
I know that the ellipses operator is used to implement variable arguments in function interface. I was wondering however if the 3 ellipses are some sort of macro or a builtin C construct. I was looking through the Mingw headers and can't seem to find any definition for this macro. I don't seem to have stdvar.h and varargs.h is empty. If indeed the ellipses are a macro can someone direct me to their implementation?
Thanks
Its not a macro. Its a part of the language itself, so a "builtin C construct".
Cant find a good reference, but google results alternate between calling them operator and specifier
I am trying to overload some operators:
/* Typedef is required for operators */
typedef int Colour;
/* Operators */
Colour operator+(Colour colour1, Colour colour2);
Colour operator-(Colour colour1, Colour colour2);
Colour operator*(Colour colour1, Colour colour2);
Colour operator/(Colour colour1, Colour colour2);
I get this error for each tried overloading:
expected '=', ',', ';', 'asm' or '__attribute__' before '+' token
I can't find any good documentation on operator overloading. Googling results in C++ tutorials which use classes. In C there are no classes. Can anyone help me? Thanks.
C does not support operator overloading (beyond what it built into the language).
You need a time machine to take you back to 1985, so that you may use the program CFront. It appears that 'C' use to support operator overloading; to the sophisticated enough it still can. See Inside the C++ Object Model by Stanley B. Lippman. OMG, C++ was C! Such a thing still exists.
This answer confirms the others. 'C' by itself does not directly support overloading. However, the important point is a programmer can write code that understands code. You need a tool that transforms source to implement this. In this case, such tools already exist.
A paper, Meta-Compilation for C++, 2001 by Edward D. Willink has interesting examples of design functionality, where extending a language is useful. The combination of *nix shell script and make rules often allow such transformation. Other examples are Qt MOC, the tools Lex and Yacc, halide etc. So while 'C' itself doesn't accommodate this directly, it does if you build host tools.
In this particular example the overloading may not make sense. However, it could make a lot of sense for a program needing arbitrary precision math.
There is no operator overloading in C.
You cannot overload these operators in C.
C does not support operator overloading at all.
You can only implement operations as functions:
Colour colour_add(Colour c1, Colour c2);
Colour colour_substract(Colour c1, Colour c2);
...
You could also switch to C++, but it may be overkill to do it just for the overloading.
Operator overloading is not available in C. Instead, you will have to use a function to "pseudo-overload" the operators:
Colour add_colours(Colour c1, Colour c2) {
return c1 + c2; // or whatever you need to do
}
If you want comparable concision, the use of macros is the best available alternative:
void Type_getSomething(int id); //or some other complex series of instructions
#define g(id) Type_getSomething(id)
...it's such a pity that the use of square brackets isn't possible for macros!
gcc 4.4.1
I am maintaining someone's code and I have come across something that I don't understand.
#define RES_API(name, func) name##_##func
Can anyone explain?
Many thanks,
The ## is a concatenation operator. Using RES_API(name1, func1) in your code would be replaced with name1_func1. More information here.
The ## operator concatenates two tokens. In your case, name is appended with an underscore, and that is appended with func.
So RES_API(aName, aFunc) results in aName_aFunc.
By itself, it seems rather annoying. I could see a use when mixing C and C++ code, as C libraries tend to prefix their functions, while C++ libraries would place them in a namespace.
Given an alternate definition, such as:
#define RES_API(name, func) name##::##func
You suddenly have a generic way to switch between a C interface, or C++.
I know you've already got your answer, but there is some great info on the C-FAQ which explains allot of the C Preprocessor magic.
Instead of doing OBJ_DoSomething, with this macro you can do RES_API(OBJ, DoSomething). Personally I think its silly.