简体   繁体   English

C / C ++中的运算符与函数

[英]Operators vs Functions in C/C++

Someone recently asked me the difference between a C++ standard operator (eg new,delete,sizeof) and function (eg tan,free, malloc). 最近有人问我C ++标准运算符(例如new,delete,sizeof)和函数(例如tan,free,malloc)之间的区别。 By "standard" I mean those provided by default by the compiler suite, and not user defined . “标准”是指编译器套件默认提供的,而不是用户定义的 Below were the answers I gave, though neither seemed satisfactory. 以下是我给出的答案,但似乎都不令人满意。

(1) An operator doesn't need any headers to be included to use it : Eg you can have a call to new without including any headers. (1)运营商不需要包含任何标头来使用它:例如,您可以拨打新电话而不包括任何标题。 However, a function (say free() ) does need headers included, compulsorily. 但是,函数(比如free())确实需要包含头文件,强制执行。

(2) An operator is defined as such (ie as a class operator) somewhere in the standard headers. (2)运算符在标准头中的某处定义为(即类操作符)。 A function isn't. 功能不是。

Can you critique these answers and give me a better idea of the difference? 你能批评这些答案并让我更好地了解它们之间的区别吗?

Operators are keywords with a fixed syntax. 运算符是具有固定语法的关键字 Those which can be overloaded might vary a bit in syntax, but that's within the boundaries. 可以重载的那些可能在语法上有所不同,但这是在边界内。 The new operator is still spelled new , even when overloaded and the syntax of invoking it is always the same. 即使在重载时, new运算符仍然是new拼写,并且调用它的语法总是相同的。

Function names are identifiers , which can be almost arbitrary. 函数名称是标识符 ,几乎可以是任意的。 There's no syntactic reason you couldn't do away with malloc() and use 没有语法上的理由你无法取消malloc()和使用

bool my_fancy_alloc(void*& memory, unsigned char size, bool zero_it);

instead. 代替。 (Mark: There are other reasons, though. Like your fellow-workers' sanity.) (马克: 还有其他原因,但就像你的同工的理智。)

(1) isn't strictly true. (1)并非严格属实。 typeid is an operator, but its use requires that you include <typeinfo> . typeid是一个运算符,但它的使用要求你包含<typeinfo>

My answer would simply be that operators are defined as such. 我的答案只是运营商被定义为这样。 :: doesn't necessarily need to be considered an operator, for instance, but the standard says it is, and so it is. ::并不一定需要被视为运营商,但标准表明它是,所以它是。

If you're looking for a more verbose answer, I would go on to mention that operators mostly do not look like functions. 如果你正在寻找一个更详细的答案,我会继续提到运营商大多看起来不像功能。 And those that do [ sizeof and typeid ] don't act as functions; 那些做[ sizeoftypeid ]的人不能充当职能; their operands are not evaluated at runtime. 它们的操作数不会在运行时进行评估。

An operator is compiled to a sequence of instructions by the compiler. 编译器将运算符编译为一系列指令。

When the code calls a function , it has to jump to a separate piece of code 当代码调用一个函数时 ,它必须跳转到一段单独的代码

运算符具有特殊语法,例如,您可以使用new (或+ )而不在运算符名称后面的括号中放置参数。

An operator and function are conceptually same. 操作员和功能在概念上是相同的。 Both of them take some values as input and return some as output. 它们都将一些值作为输入并返回一些作为输出。 They only difference is there syntax. 他们唯一的区别就是语法。

我的理解函数名称是goto运算符的表示,在跳转到特定代码位置后,应用程序可以执行一个到多个工作单元,另一方面操作员执行实际的工作单元。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM