简体   繁体   English

C 是否具有类似于 C++ 的命名空间?

[英]Does C have namespaces similar to C++?

From Programming Language Pragmatics by Michael Scott来自Michael Scott 的编程语言语用学

Modern versions of C and C++ include a namespace mechanism that provides module-like data hiding现代版本的 C 和 C++ 包括一个命名空间机制,提供类似模块的数据隐藏

Does C have namespaces similar to C++? C 是否具有类似于 C++ 的命名空间?

Are the "identifier name spaces" mentioned in C in a Nutshell the "namespaces" mentioned in Scott's book, and similar to namespaces in C++? 简而言之C 中提到的“标识符命名空间”是否是 Scott 书中提到的“命名空间”,并且类似于 C++ 中的命名空间?

Thanks.谢谢。

No , C does not have a namespace mechanism whereby you can provide “module-like data hiding”.,C没有命名空间机制,使您可以提供“模块式数据隐藏”。

book quality书籍质量
I do not know anything about the book you cited, but the word “namespaces” is one of those that gets overloaded to a lot of different meanings, just like “window”.我对你引用的那本书一无所知,但是“命名空间”这个词是一个被重载为许多不同含义的词,就像“窗口”一样。 (I question the validity of anything the author says for getting such a major point about one of the world's oldest and most widespread computer languages so brazenly wrong.) (我质疑作者所说的任何关于世界上最古老、最广泛使用的计算机语言之一的重大观点的有效性。)

name spaces in C C中的命名空间
“Name spaces” in C are a completely different mechanism, working for a completely different purpose. C 中的“命名空间”是一种完全不同的机制,用于完全不同的目的。 These are the name spaces discussed in “C in a Nutshell”.这些是“C in a Nutshell”中讨论的名称空间。 The words mean something different than C++ namespaces.这些词的含义与 C++ 命名空间不同。 Since David Rankin bothered to lookup chapter and section referencing the C11 Standard, these are the name spaces used in C:由于 David Rankin 费心查找引用 C11 标准的章节和部分,因此这些是 C 中使用的名称空间:

  • label names标签名称
  • struct/union/enum tags结构/联合/枚举标签
  • struct/union members结构/联合成员
  • everything else (including enum values)其他一切(包括枚举值)

a quick blurb about scope关于范围的简短介绍
Keep in mind that this says nothing about scope , which is a separate mechanism.请记住,这与scope无关,它是一个单独的机制。 For example, a global variable and a variable local to a function may have the same name;例如,全局变量和函数局部变量可能具有相同的名称; nevertheless they share the same name space.尽管如此,它们共享相同的名称空间。 The difference is that the global's visibility is obscured by the local variable.不同之处在于全局的可见性被局部变量遮蔽了。

value of namespaces in C++ C++中命名空间的值
It is still unclear whether namespaces were a very useful extension to C++, and the argument as to its righteousness continues.目前尚不清楚命名空间是否是 C++ 的一个非常有用的扩展,关于其正确性的争论仍在继续。 The C crowd (mostly) agrees that the headache that adding namespaces would involve doesn't justify the ends. C 人群(大多数)同意添加命名空间会涉及的头痛并不能证明结果是合理的。 I couldn't find anything particularly useful on the interwebs right off the top of my keyboard, except for a couple of bland blurbs about emulating them using structs or (even worse) using macro abuse.我在键盘顶部的互联网上找不到任何特别有用的东西,除了一些关于使用结构模拟它们或(甚至更糟)使用宏滥用的乏味宣传。 If you really want to dig, you could probably find some useful discussions archived on the comp.lang.c newsgroup.如果您真的想挖掘,您可能会在 comp.lang.c 新闻组中找到一些存档的有用讨论。

No, C has nothing like C++ namespaces.不,C 没有像 C++ 命名空间那样。 Most people have to fake what C++ does using a kind of underscore notation at best.大多数人最多只能使用一种下划线表示法来伪造 C++ 所做的事情。 This is at least what I do instead of trying to pack things into structs.这至少是我所做的,而不是试图将东西打包到结构中。 Your IDE will still help with code assists, you just have to get used to using the underscore instead of a .您的 IDE 仍将有助于代码辅助,您只需要习惯使用下划线而不是 . for everything为一切

C++ C++

MyNamespace::MyObject.myMethodOrVar ...

Ends up looking like this in C最终在 C 中看起来像这样

MyNamespace_MyObject_myMethodOrVar

May not be as smooth as C++ or Java, but it works and still helps avoid name collision.可能不如 C++ 或 Java 流畅,但它可以工作并且仍然有助于避免名称冲突。 It's just a pain in the ass.这只是屁股痛。 And yes, this doesn't give you syntactic devices like use .是的,这不会为您提供像use这样的句法设备。 It is what it is I'm afraid.恐怕就是这样了。

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

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