我有这样的代码:
#include <stdio.h>
class AbstractIterator{
virtual void do_something() = 0;
};
class AbstractList{};
class Iterator : public AbstractIterator{
public:
Iterator(const AbstractList & list) : list(list){};
virtual void do_something() override{
printf("hello\n");
};
const AbstractList & list;
};
class List : public AbstractList{
public:
Iterator getIterator(){
return Iterator(*this);
}
};
int main(int argc, char** argv){
List list;
Iterator it = list.getIterator();
it.do_something();
return 0;
}
这样可行,但我想将“推送”getIterator()方法传递给AbstractList类。为此,需要能够做到以下几点:
/* non const */
AbstractIterator &it = list.getIterator();
it.do_something();
如果没有动态分配,这可能会以某种方式完成吗?
答案 0 :(得分:6)
也许这个
class AbstractIterator{
public:
virtual void do_something() = 0;
};
class AbstractList
{
public:
virtual AbstractIterator* getIterator() = 0;
};
class Iterator : public AbstractIterator{
public:
Iterator(AbstractList& list) : list(list){}
const Iterator operator=( const Iterator& other )
{
list = other.list;
return *this;
}
virtual void do_something() override{
printf("hello\n");
}
AbstractList& list;
};
class List : public AbstractList{
Iterator iterator;
public:
List() : iterator( *this ) {}
AbstractIterator* getIterator() override
{
iterator = Iterator( *this );
return &iterator;
}
};
int main(int argc, char *argv[])
{
List list;
AbstractIterator* it = list.getIterator();
it->do_something();
return 0;
}
顺便说一句。重要的是要记住迭代器的有效性(修改列表,虚拟析构函数等),这个例子非常基础:)
写得很快
来源:
class AbstractList;
class AbstractIterator{
public:
AbstractIterator( AbstractList* list ) : list( list ), valid( true ) {}
virtual bool moveNext() = 0;
void doSomething()
{
if( isValid() )
{
do_something();
}
}
bool isValid() { return valid && 0 != list; }
void invalidate()
{
valid = false;
}
protected:
AbstractList* list;
private:
virtual void do_something() = 0;
bool valid;
};
class AbstractList
{
public:
virtual ~AbstractList()
{
for( std::shared_ptr< AbstractIterator > it : iterators )
{
it->invalidate();
}
iterators.clear();
}
std::shared_ptr< AbstractIterator > iterator()
{
std::shared_ptr< AbstractIterator > it = getIterator();
iterators.push_back( it );
return it;
}
private:
virtual std::shared_ptr< AbstractIterator > getIterator() = 0;
private:
std::list< std::shared_ptr< AbstractIterator > > iterators;
};
class Iterator : public AbstractIterator{
public:
Iterator( AbstractList* list ) : AbstractIterator(list){}
~Iterator() {printf("Iterator cleaned\n");}
virtual bool moveNext() override
{
if( !isValid() )
{
return false;
}
//do ...... iterate ... whatever
return true;
}
virtual void do_something() override
{
printf("hello\n");
}
};
class List : public AbstractList{
public:
~List()
{
printf("List cleaned\n");
}
List() {}
private:
std::shared_ptr< AbstractIterator > getIterator() override
{
std::shared_ptr< AbstractIterator > iterator( new Iterator( this ) );
return iterator;
}
};
int main(int argc, char *argv[])
{
List* list = new List();
std::shared_ptr< AbstractIterator > it = list->iterator();
it->doSomething();
if( it->isValid() )
{
std::cout << "It valid" << std::endl;
}
delete list;
if( !it->isValid() )
{
std::cout << "It !valid" << std::endl;
}
return 0;
}
这更像是应该是什么样的
答案 1 :(得分:2)
另一种解决方案可能是:
class AbstractIteratorImpl{
public:
virtual void do_something() = 0;
};
class Iterator {
pibluc:
void do_something() { impl->do_something(); }
friend class AbstractList;
private:
Iterator( std::unique_ptr<AbstractIteratorImpl> &limpl ) : impl( limpl ){}
std::unique_ptr<AbstractIteratorImpl> impl;
}
class AbstractList
{
virtual std::unique_ptr<AbstractIteratorImpl> getIteratorImpl() = 0;
public:
Iterator getIterator() { return Iterator( getIteratorImpl() ); }
};
我不确定所有参数/返回类型是否正确,但我希望这个想法很明确。
PS当然,如果你想在容器中保留迭代器的所有权,你可以使用std::shared_ptr
加上你可以在std::weak_ptr
中保留Iterator
,你就不必实现{{}明确地说,这将是自动的。
答案 2 :(得分:1)
我猜你的意思是对抽象类类型的对象的非const引用(与Java相比,C ++没有接口,它们只是纯抽象类)。
要返回引用,该对象必须在某处保持活动状态。因此,如果您的AbstractList
是一个接口(仅限抽象方法),我不知道该怎么做。