どうやら、iteratorを実装すればできるっぽいのですが、結構めんどくさいのでテンプレートとして書き留めておきます。
------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <iterator>
class AIterator;
class A{
friend AIterator;
public:
typedef AIterator iterator;
A();
virtual ~A();
A::iterator begin();
A::iterator end();
char m_str[16];
};
class AIterator : public std::iterator<std::forward_iterator_tag, int>
{
friend A;
private:
private:
AIterator();
AIterator(A* myClass, int index);
public:
AIterator(const AIterator& iterator);
public:
AIterator& operator++();
AIterator operator++(int);
int operator*();
bool operator==(const AIterator& iterator);
bool operator!=(const AIterator& iterator);
private:
int m_index;
A* m_a;
};
//
//
//
A::A()
{
m_str[0] = 0;
strcpy(m_str, "123456789");
}
A::~A()
{
m_str[0] = 0;
}
AIterator::AIterator()
{
m_a = NULL;
m_index = 0;
}
AIterator::AIterator(A* myClass, int index)
{
m_a= myClass;
m_index = index;
}
AIterator::AIterator(const AIterator& iterator)
{
m_a = iterator.m_a;
m_index = iterator.m_index;
}
int AIterator::operator*()
{
int dummy=-1;
int len;
len = strlen(m_a->m_str);
if (m_index < 0 || m_index >= len)return dummy;
dummy = m_a->m_str[m_index] & 255;
return dummy;
}
AIterator& AIterator::operator++()
{
m_index++;
return *this;
}
AIterator AIterator::operator++(int)
{
AIterator result = *this;
m_index++;
return result;
}
bool AIterator::operator!=(const AIterator& iterator)
{
return this->m_a != iterator.m_a || this->m_index != iterator.m_index;
}
bool AIterator::operator==(const AIterator& iterator)
{
return !(*this != iterator);
}
A::iterator A::begin()
{
return AIterator(this, 0);
}
A::iterator A::end()
{
int len;
len = strlen(m_str);
return AIterator(this,len);
}
//
//
//
int main()
{
A a;
for (int i : a){
printf("i=%02x\n",i);
}
return 0;
}
------------------------------------------------------------
0 件のコメント:
コメントを投稿