C++ 吗?有“与”像 Pascal 这样的关键字?
Pascal中的with
关键字可用于快速访问记录的字段。 有人知道 C++ 是否有类似的东西吗?
前任: 我有一个包含许多字段的指针,但我不想这样输入:
if (pointer->field1) && (pointer->field2) && ... (pointer->fieldn)
我真正想要的是 C++ 中的这样的内容:
with (pointer)
{
if (field1) && (field2) && .......(fieldn)
}
with
keyword in Pascal can be use to quick access the field of a record.
Anybody knows if C++ has anything similar to that?
Ex:
I have a pointer with many fields and i don't want to type like this:
if (pointer->field1) && (pointer->field2) && ... (pointer->fieldn)
what I really want is something like this in C++:
with (pointer)
{
if (field1) && (field2) && .......(fieldn)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
可能您能得到的最接近的是:(这只是一个学术练习。当然,您不能在这些人工
with
块的主体中使用任何局部变量!)或者,更多一点恶魔般地,
或在 C++0x 中
Probably the closest you can get is this: (this is just an academic exercise. Of course, you can't use any local variables in the body of these artificial
with
blocks!)Or, a bit more demonically,
or in C++0x
不,没有这样的关键字。
no there is no such keyword.
我喜欢使用:
示例:
I like to use:
Example:
在 C++ 中,您可以将代码放入由
指针
引用的类的方法中。在那里您可以直接引用成员而不使用指针。让它内联
,你几乎就得到了你想要的。In C++, you can put code in a method of the class being reference by
pointer
. There you can directly reference the members without using the pointer. Make itinline
and you pretty much get what you want.尽管我主要在 Delphi 中进行编程,它有一个
with
关键字(因为 Delphi 是 Pascal 的衍生版本),但我不使用with
。正如其他人所说:它节省了一点打字时间,但阅读却变得更加困难。在像下面的代码的情况下,可能很容易使用
with
:使用
with
这看起来像这样,我更喜欢通过引入一个指向
with
会指向同样的东西。像这样:这样就不会出现歧义,您可以节省一些打字时间,并且代码的意图比使用
with
更清晰Even though I program mostly in Delphi which has a
with
keyword (since Delphi is a Pascal derivative), I don't usewith
. As others have said: it saves a bit on typing, but reading is made harder.In a case like the code below it might be tempting to use
with
:Using
with
this looks like thisI prefer to use a different technique by introducing an extra variable pointing to the same thing
with
would be pointing to. Like this:This way there is no ambiguity, you save a bit on typing and the intent of the code is clearer than using
with
不,C++ 没有任何这样的关键字。
No, C++ does not have any such keyword.
您可以获得的最接近的是方法链接:
用于设置多个字段并
使用
对于命名空间。The closest you can get is method chaining:
for setting multiple fields and
using
for namespaces.C++没有这样的功能。许多人认为 Pascal 中的“WITH”是一个问题,因为它会使代码含糊不清且难以阅读,例如很难知道 field1 是指针成员还是局部变量或其他东西。 Pascal 还允许多个 with 变量,例如“With Var1,Var2”,这使得它变得更加困难。
C++ does not have a feature like that. And many consider "WITH" in Pascal to be a problem because it can make the code ambiguous and hard to read, for example it hard to know if field1 is a member of pointer or a local variable or something else. Pascal also allows multiple with-variables such as "With Var1,Var2" which makes it even harder.
C++中没有这样的东西。
您可以将 CODE 按原样放入 OBJECT 的方法中,但这并不总是可取的。
使用 C++11,您可以通过为 OBJECT 创建短名称的别名来非常接近。
例如,有问题的代码看起来像这样:(
周围的大括号用于限制别名
_
的可见性)如果您经常使用某个字段,您可以直接为其添加别名:
There is no such thing in C++.
You can put CODE as is into a method of OBJECT, but it is not always desirable.
With C++11 you can get quite close by creating alias with short name for OBJECT.
For example code given in question it will look like so:
(The surrounding curly braces are used to limit visibility of alias
_
)If you use some field very often you can alias it directly:
不,C/C++ 中没有
with
关键字。但您可以使用一些预处理器代码添加它:
用法:
简化的未重载定义(选择一个):
未命名(Kotlin 样式
it
):命名:
当然
for
循环始终具有只需一次传递,并将被编译器优化掉。No, there is no
with
keyword in C/C++.But you can add it with some preprocessor code:
Usage:
Simplified unoverloaded definitions (choose one):
unnamed (Kotlin style
it
):named:
Of course the
for
loop always has only a single pass and will be optimized out by the compiler.我第一次听说有人不喜欢“with”。这些规则非常简单,与 C++ 或 Java 中的类内部发生的情况没有什么不同。并且不要忽视它可以触发重大的编译器优化。
First I've heard that anybody doesn't like 'with'. The rules are perfectly straightforward, no different from what happens inside a class in C++ or Java. And don't overlook that it can trigger a significant compiler optimization.
下面的方法依赖于Boost。如果您的编译器支持 C++0x 的
auto
,那么您可以使用它并摆脱 Boost 依赖。免责声明:请不要在任何必须由其他人(甚至几个月后由您自己)维护或阅读的代码中执行此操作:
The following approach relies on Boost. If your compiler supports C++0x's
auto
then you can use that and get rid of the Boost dependence.Disclaimer: please don't do this in any code that must be maintained or read by someone else (or even by yourself in a few months):
编写了许多解析器后,这似乎是一个查找命名对象(静态或动态)的简单列表。此外,我从未见过编译器无法正确识别丢失的对象和类型的情况,因此所有那些不允许使用WITH ...ENDWITH 构造的蹩脚借口似乎都是胡言乱语。对于我们其他容易使用长对象名称的人来说,一种解决方法是创建简单的定义。无法抗拒,假设我有:
Having written numerous parsers, this seems like a dead simple list look up for the named object, either static or dynamic. Further, I have never seen a situation where the compiler did not correctly identify the missing object and type, so all those lame excuses for not allowing a WITH ...ENDWITH construction would seem to be a lot of hooey. For the rest of us prone to long object names one workaround is to create simple defines. Couldn't resist, suppose I have:
努夫说……
Nuf said ...
我可以看到一个例子,其中“with”实际上很有用。
在递归数据结构的方法中,经常会遇到这样的情况:
像这样的拼写错误导致的错误很难被发现。
使用“with”,你可以写
这可能不会超过“with”提到的所有其他负面因素,但只是作为一个有趣的信息......
I can see one instance where 'with' is actually useful.
In methods for recursive data structures, you often have the case:
errors caused by typos like this are very hard to find.
With 'with' you could write
This probably doesn't outweight all the other negatives mentioned for 'with', but just as an interesting info...
也许您可以:
或者创建一个小程序,它能够理解 C++ 中的
with
语句,并将它们转换为某种形式的有效 C++。Maybe you can:
Or create a small program that will understand
with
statements in C++ and translate them to some form of a valid C++.我也来自 Pascal 世界......而且我也喜欢 Python 使用
with
(基本上有一个自动 try/finally):这就像一个智能 ptr 对象。如果打开失败,则不会进入该块;当离开块时,文件被关闭。
这是一个非常接近 Pascal 的示例,同时也支持 Python 的使用(假设您有一个具有析构函数清理功能的智能对象);您需要更新的 C++ 标准编译器才能工作。
如果您想要更接近:
此示例的(快速而肮脏的)支持代码:
I too came from the Pascal world..... .....and I also LOVE Python's use of
with
(basically having an automatic try/finally):That acts like a smart ptr object. It does not go into the block if the open failed; and when leaving the block, the file if closed.
Here is a sample very close to Pascal while also supporting Python's usage (assuming you have a smart object with destructor cleanup); You need newer C++ standard compilers for it to work.
And if you want even closer:
The (quick and dirty) supporting code for this example:
我对 PotatoSwatter (当前接受的答案)感到遗憾,我无法使用该解决方案访问封闭范围中声明的变量。
我试图将其发布在对 PotatoSwatter 的评论回复中,但作为一个整体发布会更好。这一切都有点过头了,但是语法糖非常好!
I was lamenting to PotatoSwatter (currently accepted answer) that I could not access variables declared in the enclosing scope with that solution.
I tried to post this in a comment response to PotatoSwatter, but it's better as a whole post. It's all a bit over the top, but the syntax sugar is pretty nice!
一个简单的方法如下:
我整天都在写 python,只是在有人带我去清理之前就把它废弃了。在较大的程序中,这是如何对某些内容进行可靠计数的示例。
A simple way to do this is as follows
I've been writing python all day long and just scrapped this down before anyone takes me to the cleaners. In a larger program this is an example of how to keep a reliable count for something.