我需要存储Base类型的对象,以及派生类型BaseDerivedA和BaseDerivedB的对象。这些对象需要在内存中对齐。我想提供迭代所有对象的迭代器。我想避免存储Base指针向量的内存开销。
为此,我构建了以下容器
struct Container {
std::vector<Base> bases;
std::vector<BaseDerivedA> derivedAs;
std::vector<BaseDerivedB> derivedBs;
// Iterator over the three vectors
all_iterator<Base> all_begin(){ return all_iterator(bases[0],this); }
all_iterator<Base> end_begin(){ return all_iterator(nullptr,this); }
// Where all_iterator is defined as
template < class T >
struct all_iterator
: public boost::iterator_facade< all_iterator<T>,
T, boost::forward_traversal_tag>
{
all_iterator() : it_(0) {}
explicit all_iterator(T* p, Container* c) // THIS JUST FEELS WRONG
: it_(p), c_(c) { }
private:
friend class boost::iterator_core_access;
T* it_;
Container* c_;
void increment() {
if (it_ == static_cast<T*>(&(c_->bases[c_->bases.size()-1]))) {
it_ = static_cast<T*>(&(c_->derivedAs[0]));
} else if (it_ == static_cast<T*>(&(c_->derivedAs[ds_->derivedAs.size()-1]))) {
it_ = static_cast<T*>(&(c_->derivedBs[0]));
} else if (it_ == static_cast<T*>(&(c_->derivedBs[ds_->derivedBs.size()-1]))) {
it_ = nullptr; // THIS DOES ALSO FEEL WRONG
} else {
++it_;
}
}
bool equal(all_iterator const& other) const {
return this->it_ == static_cast<T*>(other.it_);
}
T& dereference() const { return *it_; }
};
我使用nullptr作为一个过去的结束迭代器以及大量的强制转换。我也传给我的迭代器一个指向数据结构的指针。
是否有更好的方法可以迭代三个包含Base类型的向量或从base派生的类型?
答案 0 :(得分:2)
首先,我们应该注意,如果bases
为空,您的代码会有未定义的行为;如果调用derivedBs
,则代码的大小为end_begin
。
是否有理由不能在单个容器中使用BaseType*
或智能变体,并使用抽象接口访问它而不是{{1}更明显和正常的方法} / dynamic_cast
链?然后问题就完全消失了。
编辑:如果由于某种原因需要每种类型的内存是连续的,并且您不经常单独static_cast
进入容器,只需创建一个insert
指针的容器即可指向派生对象容器内的每个对象。但是我要求你退后一步并审查为什么你需要对象是连续的(可能很容易就是合理的原因)。
答案 1 :(得分:2)
我假设BaseType是DerivedA和DerivedB的共同基础,并且您希望拥有一个包含DerivedA和DerivedB实例的容器,并且您能够在所有DerivedB实例上覆盖所有DerivedA实例并在其上进行迭代。 BaseType的所有实例(即DerivedA和DerivedB的联合)。你可以这样做:
class BaseType
{
public:
virtual void doit() const = 0;
virtual ~BaseType() { }
};
class DerivedA : public BaseType
{
public:
void doit() const { std::cout << "DerivedA::doit()" << std::endl; }
void a() const { std::cout << "DerivedA::a()" << std::endl; }
};
class DerivedB : public BaseType
{
public:
void doit() const { std::cout << "DerivedB::doit()" << std::endl; }
void b() const { std::cout << "DerivedB::b()" << std::endl; }
};
class Container
{
public:
void insert(DerivedA const & a)
{
m_as.push_back(a);
m_base.push_back(&m_as.back());
}
void insert(DerivedB const & b)
{
m_bs.push_back(b);
m_base.push_back(&m_bs.back());
}
std::vector<DerivedA>::iterator begin_a() { return m_as.begin(); }
std::vector<DerivedA>::iterator end_a() { return m_as.end(); }
std::vector<DerivedB>::iterator begin_b() { return m_bs.begin(); }
std::vector<DerivedB>::iterator end_b() { return m_bs.end(); }
std::vector<BaseType *>::iterator begin_all() { return m_base.begin(); }
std::vector<BaseType *>::iterator end_all() { return m_base.end(); }
protected:
private:
std::vector<DerivedA> m_as;
std::vector<DerivedB> m_bs;
std::vector<BaseType *> m_base;
};
答案 2 :(得分:1)
为了使你的迭代器正确,你将不得不知道你当前正在遍历哪个向量,以便你可以正确地进行比较。你可以通过枚举来告诉你哪一个是最新的:
void all_iterator::increment()
{
switch (current_member) {
case BasesMember:
++bases_iter;
if (bases_iter==bases.end()) {
current_member = DerivedAsMember;
}
return;
case DerivedAsMember:
++derived_as_iter;
if (derived_as_iter==derivedAs.end()) {
current_member = DerivedBsMember;
}
return;
case DerivedBsMember:
++derived_bs_iter;
if (derived_bs_iter==derivedBs.end()) {
current_member = EndMember;
}
return;
case EndMember:
assert(current_member!=EndMember);
break;
}
}
bool all_iterator::equal(all_iterator const &other) const
{
if (current_member!=other.current_member) return false;
switch (current_member) {
case BasesMember:
return bases_iter==other.bases_iter;
break;
case DerivedAsMember:
return derived_as_iter==other.derived_as_iter;
break;
case DerivedBsMember:
return derived_bs_iter==other.derived_bs_iter;
break;
case EndMember:
return true
}
}
Base& all_iterator::dereference() const
{
switch (current_member) {
case BasesMember: return *bases_iter;
case DerivedAsMember: return *derived_as_iter;
case DerivedBsMember: return *derived_bs_iter;
case EndMember:
assert(current_member!=EndMember);
break;
}
return *bases_iter;
}
答案 3 :(得分:0)
为什么位于derivedAs.end()
的是什么?您永远不会通过derivedAs
访问/修改它。所以你根本不需要这个假设。
典型代码是
for(auto it = derivedAs.begin(); it != derivedAs.end(); ++it) {
*it = // do whatever, will never do *derivedAs.end()
}