应该使用全局函数吗?

发布于 2024-11-08 13:40:27 字数 61 浏览 0 评论 0原文

请教一个基本问题:在 C++ 中,所有函数都应该位于类内还是非全局命名空间内?在什么情况下应该编写全局函数?

Excuse me for the elementary question: In C++, should all functions be inside a class or non-global namespace? In what sort of circumstances should one write a global function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零崎曲识 2024-11-15 13:40:27

在类外部定义函数可能对于运算符重载(特别是运算符<<)很有用,或者可能是允许使用许多类的模板函数,例如:

template <class T>
void
display (T obj)
{
  obj.display ();
}

当然,这没有说明什么在命名空间内部或外部。命名空间对于防止名称冲突很有用(例如,名为“download”的函数可以存在于多个库中,因此命名空间很有用,因为您可以使用 libraryX::download ("hello")Nlib::download ("hello") 在同一个程序中,而如果没有命名空间,编译器将无法选择要使用的函数)。但是,没有什么可以阻止您通过在命名空间之外声明函数来使函数成为全局函数,事实上,这很常见。如果您正在创建一个库,我建议使用命名空间来防止另一个库使编译器感到困惑。

Defining a function outside a class might be useful for operator overloading (specifically operator<<) or perhaps a template function that allows many of your classes to be used such as:

template <class T>
void
display (T obj)
{
  obj.display ();
}

Of course, that says nothing about being inside or outside a namespace. Namespaces are useful to prevent name clashes (e.g. a function named "download" can be present in multiple libraries, so namespaces are useful in that you can use libraryX::download ("hello") and Nlib::download ("hello") in the same program whereas without a namespace the compiler would be unable to pick which function to use). However, there is nothing preventing you from making a function global by declaring the function outside of a namespace, and in fact this is quite common. If you're creating a library, I'd recommend using a namespace to prevent another library from confusing the compiler.

2024-11-15 13:40:27

这取决于你。如果您来自更严格的面向对象语言,例如 Java,您会发现使用全局函数而不是类方法可能是不好的风格,但是全局函数有很多用例。只需按照您想要的方式进行编码即可,C++ 对于不同的编程范例和风格非常自由。

如果它在语义上应该是一个全局函数,那么只需将其设为一个,而不是方法或静态方法。运算符和具有运算符语义的普通函数就是一个很好的例子。看看STL算法,为什么是全局的?因为它们不属于单个容器。为什么它们不属于仅具有静态方法的类?因为它没有任何优点,而不是用面向对象的幻想来迷惑大家。

编辑:好吧,我想命名空间是另一个故事。当您设计一个库或包含在您还不知道的其他代码中的内容时,将内容打包到命名空间中以避免污染全局命名空间可能总是一个好主意。但至少运算符不应该位于命名空间中,正如我认为的那样(如果我错了,请纠正我),否则如果没有using namespace,它们将无法工作,这在大多数情况下是一个坏主意。但构建独立程序时,实际上不需要自己的命名空间。

It is up to you. If you are coming from a more strictly object-oriented language like Java, you will find it perhaps bad style to use global functions instead of class methods, but there are plenty of use-cases for global functions. Just code it like you want it, C++ is quite liberal concerning different programming paradigms and styles.

If it should be a global function semantically, then just make it one instead of a method or a static method. Operators and normal functions with operator semantics are a good example. Look at the STL algorithms, why are they global? Because they do not belong to a single container. Why are they not part of a class with only static methods? Because there is no advantage in it, instead of confusing everybody with the illusion of object-orientation.

EDIT: Ok, I suppose namespaces are another story. When you are designing a library or something that is included in other code you do not know yet, it is probably always a good idea to pack things into namespaces to avoid pollution of the global namespace. But at least operators shouldn't be in a namespace, as I think (but correct me if I'm wrong) that otherwise they won't work without using namespace, which is most times a bad idea. But when building an independent program, there is actually no need for your own namespaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文