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

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

问题描述

我已经在网上阅读了很多文章和文章,但是我找不到关于这个的确切答案。

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.

现在有三种方式:


  • 创建一个包含静态的类(contra:potential无法调用没有对象的成员函数 - 并非一切都需要是静态的)

  • (没有私人关键字 - 为什么我应该把它放在一个命名空间,然后呢?)
  • 创建一个单例类(对我有用)。
    <
  • making a class with everything being static (contra: potential "Cannot call member function without object" - not everything needs to be static)
  • making a singleton class (contra: I WILL need the object)
  • making a namespace (no private keyword - why should I put it in a namespace at all, then?)

我的方法是什么?可能的方式组合这些方式?

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),所以人们不会突然构建您的对象的实例:

  • b $ b
  • because people won't suddenly build instances of your objects: to what end ?
  • because no introspection information (RTTI) is generated for namespaces

这是一个典型的实现:

// 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

匿名命名空间文件是一个增强的私人部分:不仅客户端不能使用里面的内容,但他甚至根本看不到它(因为它在源文件中),因此不依赖于它(它具有明确的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 ++,静态vs.命名空间vs.单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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