質問

次のようなコンテナクラスに取り組んでいます:

class hexFile {
public:
    HANDLE theFile;
    unsigned __int64 fileLength;
    hexFile(const std::wstring& fileName)
    {
        theFile = CreateFile(fileName.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
        if (theFile == INVALID_HANDLE_VALUE);
        {
            throw std::runtime_error(eAsciiMsg("Could not open file!"));
        }
        BY_HANDLE_FILE_INFORMATION sizeFinder;
        GetFileInformationByHandle(theFile, &sizeFinder);
        fileLength = sizeFinder.nFileSizeHigh;
        fileLength <<= 32;
        fileLength += sizeFinder.nFileSizeLow;
    };
    ~hexFile()
    {
        CloseHandle(theFile);
    };
    hexIterator begin()
    {
        hexIterator theIterator(this, true);
        return theIterator;
    };
    hexIterator end()
    {
        hexIterator theIterator(this, false);
        return theIterator;
    };
};

そして、次のような一致するイテレータクラス:

class hexIterator : public std::iterator<std::bidirectional_iterator_tag, wchar_t>
{
    hexFile *parent;
public:
    bool highCharacter;
    __int64 filePosition;
    hexIterator(hexFile* file, bool begin);
    hexIterator(const hexIterator& toCopy);
    ~hexIterator();
    hexIterator& operator++()
    {
        return ++this;
    }
    hexIterator& operator++(hexIterator& toPlus);
    hexIterator& operator--()
    {
        return --this;
    }
    hexIterator& operator--(hexIterator& toMinus);
    hexIterator& operator=(const hexIterator& toCopy);
    bool operator==(const hexIterator& toCompare) const;
    bool operator!=(const hexIterator& toCompare) const;
    wchar_t& operator*();
    wchar_t* operator->();
};

私の問題は...両方のクラスが他のクラスで実装する必要があるということです。たとえば、イテレータの内部でコンテナを参照する方法がわかりません。なぜなら、イテレータが定義されたとき、コンテナはまだ定義されていないからです。

これをどのように達成できますか?

Billy3

役に立ちましたか?

解決

前方に宣言します。別のクラスの宣言で前方宣言されたクラスへの参照を使用できるため、これは機能するはずです:

class hexFile; // forward

class hexIterator : ,,, {
   ...
};

class hexFile {
   ...
};

他のヒント

前方参照を使用して .h ファイルを開始します。

class hexFile;

その後、 class hexIterator の完全な定義( hexFile へのポインターのみが必要なためコンパイルされます) class hexFile の定義(この時点でコンパイラは hexIterator のすべてを知っているため、今では問題なくコンパイルされます)。

.cpp ファイルでは、もちろん .h をインクルードしているので、すべてが既知であり、任意の順序でメソッドを実装できます。

定義をクラスの宣言から分離することをお勧めします。ヘッダーファイルで、hexFileクラスを前方宣言し、ヘッダーファイルで両方を完全に宣言します。その後、関連するソースファイルで個々のクラスをより詳細に定義できます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top