简体   繁体   English

为什么new是C ++中的运算符

[英]Why new is an operator in C++

I am just confused between operator vs keywords . 我只是在operatorkeywords之间混淆。 I know this doesn't matter for programming, but I want to know the difference. 我知道这对编程无关紧要,但我想知道其中的区别。

In Java new is called as keyword. 在Java中, new被称为关键字。 But in c++ new is called as an operator, Why? 但是在c ++中, new被称为运算符,为什么呢?

new is a keyword in C++ (see the table in C++11 N3337 2.12/1). new是C ++中的关键字(参见C ++ 11 N3337 2.12 / 1中的表)。 It then uses operator new as its implementation, a side effect of which allows programmers to create their own overloaded operator new to inject custom memory management. 然后它使用operator new作为其实现,其副作用允许程序员创建自己的重载operator new以注入自定义内存管理。

Keywords are grammatical constructs. 关键词是语法结构。 They are specific sequences of characters that the parser inherently recognizes, which cannot be used as identifiers. 它们是解析器固有识别的特定字符序列,不能用作标识符。 if is a keyword. if是关键字。 So is new , even in C++. 所以是new ,即使在C ++中也是如此。

The term "operator" is used to identify things which are used as parts of expressions. 术语“运算符”用于标识用作表达式部分的内容。 + is an operator; +是运营商; it is acts on two expressions and creates a value from them. 它作用于两个表达式并从中创建一个值。 new is also an operator; new 也是一个运营商; it takes parameters and a typename+constructor parameters, and performs various operations on them, resulting in the creation of a new object of that type. 它接受参数和typename +构造函数参数,并对它们执行各种操作,从而创建该类型的新对象。

But if is not an operator. if不是运营商。 Oh sure, an if statement involves expressions, but if itself is not part of an expression. 哦,当然, if语句涉及表达式,但if它本身不是表达式的一部分。

Your specific example is a bit misleading. 您的具体示例有点误导。 new is both a keyword and an operator, in both C++ and Java. new在C ++和Java中都是关键字和运算符。 These terms also aren't strictly defined: they're contextually defined. 这些术语也没有严格定义:它们是在上下文中定义的。

keyword s are any tokens that appear in a program that are defined by the compiler (though custom keywords can be set by the user by tweaking the compiler, if the compiler allows it). keyword s是出现在由编译器定义的程序中的任何标记(尽管用户可以通过调整编译器来设置自定义关键字,如果编译器允许的话)。 Something like typedef in C/C++ is a keyword, and depending on the language, fundamental types can be treated as keywords as well. 像C / C ++中的typedef这样的东西是一个关键字,根据语言的不同,基本类型也可以被视为关键字。 The usual signifier of a keyword is that the compiler won't let you name variables or functions after them (though there are exceptions and caveats to that rule). 关键字的通常表示符是编译器不允许在它们之后命名变量或函数(尽管该规则存在例外和警告)。

operator s are any action performed on your data or objects in code. operator s是对代码中的数据或对象执行的任何操作。 The most basic are the arithmetic operators associated with the fundamental data types, like + - * / % = == || && 最基本的是与基本数据类型相关的算术运算符,如+ - * / % = == || && + - * / % = == || && and so on. + - * / % = == || &&等等。 new (and, in C++, delete ) are also operators, because they perform specific tasks on the objects they target (specifically, they allocate and delete heap memory). new (以及在C ++中, delete )也是运算符,因为它们对目标对象执行特定任务(具体来说,它们分配和删除堆内存)。 The . . (dot) is an operator, and there's even been a few proposals in C++ to allow the user to overload the dot-operator! (dot)是一个运算符,在C ++中甚至有一些提议允许用户重载点运算符!

A lot of this is a distinction without difference though. 然而,很多这是区别,但没有区别。 It's way more useful to know what new and new[] do than to know whether they are strictly classified as operators or not (though to be clear, they are), or whether all operators are keywords or not (I think they are, but I'm not sure) or whether all keywords are operators (I don't believe they are). 了解newnew[]做什么比知道它们是否被严格归类为运营商(虽然很清楚,它们是),或者是否所有运营商都是关键词(我认为它们是,但是我不确定)或者所有关键字都是运营商(我不相信)。 If you find a case study where those distinctions matter, I'd love to know of them. 如果你找到一个案例研究,这些区别很重要,我很想知道它们。

In my understanding keywords are reserved word of the language, while operators are "implemented language functions". 在我看来, 关键词是语言的保留词 ,而操作符是“实现语言功能”。

It happens that some keywords are also operators . 碰巧有些关键字也是运营商

instanceof is another example of keyword that is also an operator. instanceof是关键字的另一个例子,它也是一个运算符。

Other example are the arithmetic operators, + - * % are operators too but not keywords . 其他示例是算术运算符, + - * % 也是运算符, 但不是关键字

I believe it applies to most languages, Java, C++ and others. 我相信它适用于大多数语言,Java,C ++等。

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

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