我正在研究一个看起来像这样的容器类:
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->();
};
我的问题是......两个类都需要根据另一个类来实现。我不确定如何在迭代器中引用容器,例如,因为定义迭代器时,容器还没有定义。
怎么可能做到这一点?
比利3