
EmptyIterator는 사용자가 비어있는 컬렉션을 반복적으로 처리할 때 사용됩니다. EmptyIterator::current 메서드는 현재 반복 중인 요소를 반환합니다. EmptyIterator::current가 반환하는 값은 nullptr입니다.
EmptyIterator::current가 반환하는 값이 nullptr인 이유는 EmptyIterator가 비어있는 컬렉션을 처리하기 때문에 현재 반복 중인 요소가 없기 때문입니다.
EmptyIterator::current 메서드를 사용할 때 주의해야 할 점은 nullptr을 반환할 수 있기 때문에 null pointer exception이 발생하지 않도록 null 체크를 수행해야 합니다.
EmptyIterator::current를 사용하여 Iterator를 만들기 위한 방법은 EmptyIterator::current를 사용하여 Iterator의 생성자를 호출하는 것입니다.
EmptyIterator::current를 사용하여 Iterator를 만들기 위한 예제 코드는 다음과 같습니다.
#hostingforum.kr
cpp
#include
#include
class EmptyIterator {
public:
bool hasNext() { return false; }
void* current() { return nullptr; }
};
class MyIterator {
public:
MyIterator(void* current) : current_(current) {}
void* current() { return current_; }
private:
void* current_;
};
int main() {
EmptyIterator emptyIterator;
MyIterator iterator(emptyIterator.current());
if (iterator.current() == nullptr) {
std::cout << "nullptr" << std::endl;
}
return 0;
}
2025-06-28 10:06