C++仅静态类

发布于 2024-12-07 20:46:58 字数 171 浏览 0 评论 0原文

我有一堆函数,我想将它们放入单独的命名空间或类中。两者相比,哪个更好?如果是后者,我该怎么办?我的意思是,我没有任何实例成员,所以我应该将构造函数指定为私有吗?或者删除它?

无论哪种方式,它看起来都会像这样给你一个想法:

myproject::foo::f()

I have a bunch of functions that I want to put in either a separate namespace or a class. Out of the two, which would be better? If it's the latter, how should I go about it? I mean, I don't have any instance members, so should I specify the constructor as private? Or delete it?

Either way, it will look something like this to give you an idea:

myproject::foo::f()

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

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

发布评论

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

评论(2

一个人的旅程 2024-12-14 20:46:58

C++ 命名空间和类都可以包含函数和其他类型,但在行为上存在一些关键差异。您必须决定哪一个最适合您的特定情况。

  • 类可以模板化,但命名空间不能。这提供了一种将一组模板参数传递给整组函数和其他类型的方法。与 typedef 结合使用,功能非常强大。

  • 您可以使用using namespace xyz;将整组命名空间成员引入任何范围。可以继承类以将它们纳入范围(由于空基优化,如果没有实例成员,则没有太大影响,但静态成员可以在没有限定的情况下使用),但这仅适用于其他类。自由函数必须显式限定对类成员的所有访问。

  • 命名空间参与依赖于参数的查找,而父类的成员不参与。

如果您想使用继承类的技巧来提供模板参数并获得对其静态成员的非限定访问,那么您应该将构造函数和析构函数保留为默认且微不足道的,而不是删除它们或使它们不可访问。在 C++11 中,您可以将构造函数标记为受保护的和默认的。

C++ namespaces and class can both contain functions and other types, but there are some key differences in behavior. You'll have to decide which is best for your particular circumstance.

  • Classes can be templated, namespaces can't. This provides a way to pass a set of template arguments to a whole group of functions and other types. Combined with typedef, this can be very powerful.

  • You can use using namespace xyz; to bring a whole group of namespace members into any scope. Classes can be inherited to bring them into scope (has not much effect if there are no instance members, due to the empty base optimization, but static members can then be used without qualification), but this only works inside other classes. Free functions would have to explicitly qualify all access to members of the class.

  • Namespaces participate in argument-dependent lookup, members of a parent class don't.

If you want to use the trick of inheriting a class to supply template arguments and gain unqualified access to its static members, then you should leave the constructor and destructor as defaulted and trivial, instead of deleting or making them inaccessible. In C++11, you can mark the constructor both protected and defaulted.

爱*していゐ 2024-12-14 20:46:58

如果您使用该类,最好将默认构造函数、复制构造函数、赋值运算符和析构函数声明为私有/未实现(对于 C++11,可能还需要更多)。如果不这样做,您将拥有一个可用于创建无用对象的类。如果您使用名称空间,则不必执行任何操作。

类是为对象创建的。将它们用作静态函数的容器,没有成员数据,滥用多于使用。

If you use the class it is best to declare the default constructor, copy constructor, assignment operator, and destructor private / unimplemented (plus maybe some more for C++11). If you don't you'll have a class that can be used to make useless objects. If you use a namespace you don't have to do any of that.

Classes are made for objects. Using them as containers of static functions, no member data, is more abuse than use.

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