避免头文件'污染' [英] Avoiding header file 'pollution'

查看:271
本文介绍了避免头文件'污染'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一个类叫窗口,它设计用于照顾窗口的所有要求,它的工作奇妙。我唯一的问题是,我包括window.h中的任何文件也获取所有的窗口包括。我已经把所有的windows特定的元素私有,所以没有真正的理由,我的项目的其余部分需要知道这些元素,我宁愿知道超越一个怀疑的阴影,我没有意外地意外地生成了其他位和窗口特定的事件。

Okay, I have a class called a Window which is designed to take care of all of the requirements of putting together a window and it works wonderfully. My only issue is that any file that I include window.h in also gets all of the windows includes as well. I've made all of the windows-specific elements private, so there's no real reason that the rest of my project needs to know about those elements and I'd rather know beyond a shadow-of-a-doubt that I've not accidentally made other bits and pieces windows-specific on accident.

我已经能够找出这样做的唯一方法是创建一个第二个类包含这些元素,并在第一个类中放置一个指向实例的指针。对于我说的例子,让我们从下面的文件ElementsIWantHidden.h开始。

The only way that I've been able to figure out to do this would be to create a second class that contains these elements and place a pointer to an instance within the first class. For an example of what I'm talking about, lets start off with the following file "ElementsIWantHidden.h"

#ifndef ELEMENTS_I_WANT_HIDDEN_H
#define ELEMENTS_I_WANT_HIDDEN_H

typedef short int16;
typedef int int32;
typedef long long int64;

#endif

假设这些是一些定义其他功能,类似于windows.h如何定义WHND。我需要Windows函数,我需要我的窗口类来使用它们,但我不想犯错误使用一些这些定义在我的Windows类之外。

Lets assume that these are some defines that come with some other functions, similar to how windows.h defines WHND. I need the windows functions and I need my window class to use them, but I don't want to make the mistake of accidentally using some of these defines outside of my windows class.

如果我使用以下代码的文件TraditionalClass.h:

If I have the file "TraditionalClass.h" with the following code:

#ifndef TRADITIONAL_CLASS_H
#define TRADITIONAL_CLASS_H

#include "ElementsIWantHidden.h"

class TraditionalClass
{
private:
    int16 shortInt;
    int32 mediumInt;
    int64 longInt;
public:
    TraditionalClass();
    void print();
};

#endif

那么包含TraditionalClass.h的任何文件也包括windows .h,必须编译windows.h,并且有所有的windows函数和类型可用。

then any file that includes TraditionalClass.h also includes windows.h, has to compile windows.h, and has all of the windows functions and types available to it.

我能够找出的唯一解决方法是具有类似文件ClassWithHiddenElements.h的代码:

The only workaround I've been able to figure out is to have something like the file "ClassWithHiddenElements.h" with the code:

#ifndef CLASS_WITH_HIDDEN_ELEMENTS
#define CLASS_WITH_HIDDEN_ELEMENTS

class HiddenElementsContainer;

class ClassWithHiddenElements
{
private:
    HiddenElementsContainer* hiddenElementsContainer;
public:
    ClassWithHiddenElements();
    ~ClassWithHiddenElements();
    void print();
};

#endif

ClassWithHiddenElements.cpp然后定义或包括一个文件, code> HiddenElementsContainer 并在构造函数中创建和实例,并在析构函数中删除它。

ClassWithHiddenElements.cpp then defines or includes a file defining HiddenElementsContainer and in the constructor creates and instances and in the destructor deletes it.

这个工作和类包括ClassWithHiddenElements。 h看不到ElementsIWantHidden.h中定义的任何内容。问题是,使用这些类型的ClassWithHiddenElements的成员必须隐藏在减慢访问速度的指针后面。 ClassWithHiddenElements现在必须有一个复制构造函数,复制操作符和析构函数。当更多的内存被分配来创建ElementsIWantHidden的实例时,就有可能通过构造ClassWithHiddenElements部分地耗尽内存,这意味着构造函数不能有一个无引用的保证。其中一些不是大问题,因为还有很多其他的东西,往往会创建这些。是否有更好的方法来完成这个目标?我需要包括来自windows.h的类型的实例变量,但我不想它污染的项目的其余部分。

This works and classes that include ClassWithHiddenElements.h can't see anything defined within ElementsIWantHidden.h. The problem is that members of ClassWithHiddenElements that use those types must be hidden behind a pointer which slows down accessing. ClassWithHiddenElements must now have a copy constructor, copy operator, and destructor. There is the potential to run out of memory part way through constructing ClassWithHiddenElements when more memory gets allocated to create the instance of ElementsIWantHidden which means the constructor can't have a no-throw guarentee. Some of these aren't big issues, as there're a lot of other things which will often create these. Is there a better way to accomplish this goal though? I need to include instance variables of types from windows.h, but I don't want it to pollute the rest of the project.

FYI,这是为游戏开发并且我想最终的产品是跨平台兼容,这是很大一部分原因,我不想要的windows.h的元素意外使用在其他文件,不应该是操作系统意识。 / p>

FYI, this is for game development and I want the final product to be cross-platform compatible, which is a large part of the reason that I don't want elements of windows.h to get accidentally used in other files that aren't supposed to be OS aware.

推荐答案

typedefs 作为课程的一部分:

class TraditionalClass
{
private: // can make them public also
    typedef short int16;
    typedef int int32;
    typedef long long int64;

private:
    int16 shortInt;
    int32 mediumInt;
    int64 longInt;
public:
    TraditionalClass();
    void print();
};

这种方式在类中可以简单地引用你的类型没有任何资格名称。

This way inside the class you can refer to your types simply without any qualification name.

从类外面,如果 private ,就不能访问它们。如果它们 public ,您只能使用其限定名称访问它们: TraditionalClass :: int16

From outside the class, you can't access them at all if they are private. If they are public you can access them with their qualified name only: TraditionalClass::int16.

另一种方法是创建一个命名空间,并将类和 typedefs 放在该命名空间中。无论你把typedef放在哪里,都应该为你的类创建一个命名空间。

Another way is to create a namespace and put both the class and the typedefs in that namespace. Regardless of where you put the typedefs, you should create a namespace for your class.

这篇关于避免头文件'污染'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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