使用声明应该有多窄? [英] How narrow should a using declaration be?

查看:115
本文介绍了使用声明应该有多窄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个小类 widget 使用 std :: string 。它在许多地方使用它,通常与 std :: vector 结合使用。



我想使用使用关键字,即使用std :: string;



问题是,放置它的最佳位置在哪里? p>

  // widget.h文件
#ifndef WIDGET
#define WIDGET

//(1)
命名空间示例{
//(2)

命名空间嵌套{
//(3)

widget {
public:
//(4)
...
private:
//(5)
std :: string name_;
...
};

}
}

#endif


$ b

我的问题是:


  1. 如果我将它放在(1)那么包含 widget.h 的每个人的范围都会被 string

  2. (2)(3),这是与1中的相同的故事只有命名空间示例 example :: nested 将在包含 widget.h的第二个文件中被污染(5)

  3. c $ c>,声明是相当孤立的,但会在实现(Cpp)文件和继承类中可见吗?

提前感谢!

解决方案

不要在(1)中执行。

每个人都会诅se一千年的名字。

作为你的类的用户,我不介意你污染你自己的命名空间。但是,如果你污染我的任何命名空间(包括全局),我会感到不安,因为这将影响我的代码是如何编译的。 为什么是'using namespace std;'考虑了C ++中的不良做法?



您不能在(4)或(5)上使用它。



由于我(个人)希望尽可能接近使用点(防止污染)。

你最好做的是(3)。 p>

但我甚至不会这样做。我明确的标准的任何东西。但我会typedef我的容器类型。

  private://(所以在5)不需要公开内部细节你的班。 
typedef std :: vector< std :: string> MyCont;

这是一个更好的技术,因为你只需要在一个地方进行更改, 。

  //如果您更改
//现在不再需要修改sub typef了容器的类型。只需更改容器typedef和
//现在迭代器会自动更正。
public://(所以在4)迭代器是公共的(不公开实现)。
typedef MyCont :: iterator iterator;
typedef MyCont :: const_iterator const_iterator;


I have this little class widget that uses a std::string. It uses it in many places, often in conjunction to a std::vector. So you can see, that the typenames become very long and annoying.

I want to utilize the using keyword, i.e. using std::string;

The question is, where is the best place to place it?

// widget.h file
#ifndef WIDGET
#define WIDGET

// (1)
namespace example {
    // (2)

    namespace nested {
        // (3)

        class widget {  
        public:
            // (4)
            ...
        private:
            // (5)
            std::string name_;
            ...
        };

    }
}

#endif

My questions are:

  1. If I place it in (1) then everybody who includes widget.h will have their scope polluted with string?
  2. In places (2) and (3), it's the same story as in 1. only that the namespaces example and example::nested will be polluted in a second file that includes widget.h?
  3. In places (4) and (5), the declaration is quite isolated, but will it be visible in the implementation (Cpp) file and in inheriting classes?

Thanks in advance!

解决方案

Do not do it in (1).
Everybody will curse your name for a thousand years.
As a user of your class I don't mind you polluting your own namespace. But I will be upset if you pollute any of my namespaces (including global) as this will affect how my code is compiled. Why is 'using namespace std;' considered a bad practice in C++?

You can't use it at (4) or (5).

Since I (personally) would want to bind it as close as possible to the point of usage (to prevent pollution).
The best you can do is (3).

But I would not even do that. I am explicit about anything from standard. But I would typedef my container type.

private: //(so at 5) Don't need to expose internal details of your class.
    typedef std::vector<std::string>   MyCont;

This is a better technique as you will only need to make a change in one place and changes will cascade.

// Sub typedefs now will no longer need to change if you change
// The type of container. Just change the container typedef and
// now the iterators are automatically correct.
public: //(so at 4)  Iterators are public (and not exposing the implementation).
    typedef MyCont::iterator       iterator;
    typedef MyCont::const_iterator const_iterator;

这篇关于使用声明应该有多窄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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