在哪里定义复杂映射的 typedef ?

发布于 2024-07-27 23:02:01 字数 211 浏览 2 评论 0原文

一般问题:通常在哪里定义类、命名空间。

我的情况:

我只在我的类实现中使用这个映射:

std::map<const std::pair<string, string>,const string*>

我想知道将它放在类中(公开:)或封闭的头文件中的好地方在哪里? 我没有命名空间

General question: Where is usually defined class, namespace.

My case:

I am only using this maps in my class implementation:

std::map<const std::pair<string, string>,const string*>

and I wonder where is it good place to put it in the class (in public:) or in the enclosing header file? I don't have namespaces

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

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

发布评论

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

评论(3

幸福丶如此 2024-08-03 23:02:04

这取决于,您是否需要像类的公共接口中那样的类型? 如果是这样,那么我会在类的公共部分中声明它。 如果您只在类的私有部分使用它,那么在那里声明它。 如果您仅在实现中使用它,则在那里声明它。 一般来说,永远不要在头文件的全局命名空间中声明它(或者如果您确实确保在名称前添加不会冲突的内容)。

这里的经验法则是使减速尽可能对程序的最小子集可见/可访问。

It depends, do you need this type as in the public interface to the class? If so then I would declare it inside the public section of the class. If you only use it in the private part of the class then declare it there. And if you only use it in the implementation then declare it there. In general never declare it in global namespace in a header file (or if you do make sure to prefix the name with something that won't clash).

The rule of thumb here is to make the deceleration visible/accessible to the smallest subset of your program as possible.

撩起发的微风 2024-08-03 23:02:03

如果它仅在您的班级中,我通常将其放在私有部分的顶部:

class Foo
{
public:
    void some_functions(void);

private:
    typedef std::pair<std::string, std::string> StringPair;
    typedef std::map<StringPair, std::string> StringPairMap;

    StringPairMap _stringMap;
}

为了澄清,与大多数事情一样,您希望将这些事情指定为尽可能本地化。 变量应该这样做:在到达循环之前,不要为 for 循环定义 int i ,等等。

同样,如果您的类在内部使用这些 typedef,请执行我上面所说的操作。 如果类中只有一个特定函数需要该 typedef,请将 typedef 放在该函数的开头。

如果您需要将此 typedef 公开给类的客户端,我喜欢将 typedef 放置在 public 部分的顶部。

If it is in your class only, I usually put it at the top of the private section:

class Foo
{
public:
    void some_functions(void);

private:
    typedef std::pair<std::string, std::string> StringPair;
    typedef std::map<StringPair, std::string> StringPairMap;

    StringPairMap _stringMap;
}

To clarify, as with most things you want to specify these things as local as possible. Variables should do this: you don't define int i for your for-loop until you've reached the loop, etc..

Likewise, if your class uses these typedef's internally, do what I said above. If only a specific function in your class needs that typedef, place the typedef at the beginning of that function.

If you need to expose this typedef to clients of the class, I like to place the typedef's at the top of the public section.

怀里藏娇 2024-08-03 23:02:03

把它放在你使用的地方。 如果您在头文件的私有部分中使用它,请在那里声明 typedef。 如果仅在 .cpp 文件的实现代码中使用它,请在那里声明 typedef。

Put it where you use it. If you use it in the private section of the header file, declare the typedef there. If you use it only in the implementation code in the .cpp file, declare the typedef there.

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