C++,静态与命名空间与单例 [英] C++, static vs. namespace vs. singleton

查看:31
本文介绍了C++,静态与命名空间与单例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在网上阅读了很多帖子和文章,但我找不到明确的答案.

I already read a lot of posts and articles all over the net, but I couldn't find a definite answer about this.

我有一些具有类似目的的函数,我想在全局范围之外使用.其中一些需要是公开的,其他应该是私有的(因为它们只是公共"函数的辅助函数).此外,我不仅有函数,还有变量.它们只有私有"辅助函数需要,也应该是私有的.

I have some functions with similar purposes that I want to have out of the global scope. Some of them need to be public, others should be private (because they are only helper functions for the "public" ones). Additionally, I don't have only functions, but also variables. They are only needed by the "private" helper functions and should be private, too.

现在有三种方式:

  • 创建一个所有内容都是静态的类(相反:潜在的不能在没有对象的情况下调用成员函数" - 并非所有内容都必须是静态的)
  • 创建一个单例类(相反:我需要这个对象)
  • 创建一个命名空间(没有私有关键字 - 那我为什么要把它放在一个命名空间中呢?)

我该怎么做?组合这些方式的可能方式?

What would be the way to take for me? Possible way of combining some of these ways?

我想到了类似的东西:

  1. 制作单例,静态函数使用单例对象的辅助函数(这可能吗?我仍在类中,但访问它类型的对象)
  2. 在程序启动时调用的构造函数,初始化所有内容(-> 确保静态可以从单例对象访问函数)
  3. 只能通过 MyClass::PublicStaticFunction() 访问公共函数

谢谢.

推荐答案

如前所述,使用全局变量通常是不好的工程实践,当然除非绝对需要(例如映射硬件,但这并不经常发生).

As noted, using global variables is generally bad engineering practice, unless absolutely needed of course (mapping hardware for example, but that doesn't happen THAT often).

将所有内容存储在类中是您在类似 Java 的语言中会做的事情,但在 C++ 中您不必这样做,事实上,在这里使用命名空间是一个更好的选择,如果只是:

Stashing everything in a class is something you would do in a Java-like language, but in C++ you don't have to, and in fact using namespaces here is a superior alternative, if only:

  • 因为人们不会突然构建您的对象的实例:为了什么目的?
  • 因为没有为命名空间生成自省信息 (RTTI)

这是一个典型的实现:

// foo.h
#ifndef MYPROJECT_FOO_H_INCLUDED
#define MYPROJECT_FOO_H_INCLUDED

namespace myproject {
  void foo();
  void foomore();
}

#endif // MYPROJECT_FOO_H_INCLUDED

// foo.cpp
#include "myproject/foo.h"

namespace myproject {

namespace {
  typedef XXXX MyHelperType;

  void bar(MyHelperType& helper);
} // anonymous

void foo() {
  MyHelperType helper = /**/;
  bar(helper);
}

void foomore() {
  MyHelperType helper = /**/;
  bar(helper);
  bar(helper);
}
} // myproject

巧妙地隐藏在源文件中的匿名命名空间是增强的 private 部分:不仅客户端无法使用其中的内容,而且他甚至根本看不到它(因为它在源文件中),因此不依赖它(它具有明确的 ABI 和编译时优势!)

The anonymous namespace neatly tucked in a source file is an enhanced private section: not only the client cannot use what's inside, but he does not even see it at all (since it's in the source file) and thus do not depend on it (which has definite ABI and compile-time advantages!)

这篇关于C++,静态与命名空间与单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆