gpt4 book ai didi

c++ - 返回对类的引用

转载 作者:太空宇宙 更新时间:2023-11-04 14:53:54 25 4
gpt4 key购买 nike

我有这样的代码:

#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();

这是否可以在没有动态分配的情况下以某种方式完成?

最佳答案

也许是这样

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;
}

顺便说一句。重要的是要记住迭代器的有效性(列表的修改、虚拟析构函数等),这个例子非常基础:)

这个写的很快

  • 迭代器的失效函数应该是私有(private)的,可以做到,但会使源代码复杂化
  • 注意接口(interface)方法是通过模板方法调用的——好习惯

来源:

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;
}

这更应该是什么样子

关于c++ - 返回对类的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31180761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com