克隆分配器,以及 boost::p tr_container 中的多态性
Clone allocators, and polymorphism in boost::ptr_container
对于我当前的项目,我正在使用boost::ptr_vector
来多态地保存Objects
,并且一切正常,直到我的一个构建VS2010抛出它无法克隆object
所以我去了,并查找克隆,并按照boost
的要求实现了new_clone()
方法,并按照c ++常见问题解答使其pure virtual
, 但是现在VS2010向我抛出这个shanagin,说它不能在抽象类上使用克隆。
1>c:program filesboostboost_1_49_0boostptr_containerclone_allocator.hpp(34): error C2259: 'Object' : cannot instantiate abstract class
1> due to following members:
1> 'Object *Object::new_clone(void) const' : is abstract
1> c:...Object.h(36) : see declaration of 'Object::new_clone'
1> c:program filesboostboost_1_49_0boostptr_containerclone_allocator.hpp(68) : see reference to function template instantiation 'T *boost::new_clone<U>(const T &)' being compiled
1> with
1> [
1> T=Object,
1> U=Object
1> ]
1> c:program filesboostboost_1_49_0boostptr_containerdetailreversible_ptr_container.hpp(110) : see reference to function template instantiation 'U *boost::heap_clone_allocator::allocate_clone<Object>(const U &)' being compiled
1> with
1> [
1> U=Object
1> ]
1> c:program filesboostboost_1_49_0boostptr_containerdetailreversible_ptr_container.hpp(99) : while compiling class template member function 'Object *boost::ptr_container_detail::reversible_ptr_container<Config,CloneAllocator>::null_clone_allocator<allow_null_values>::allocate_clone(const Object *)'
1> with
1> [
1> Config=boost::ptr_container_detail::sequence_config<Object,std::vector<void *,std::allocator<void *>>>,
1> CloneAllocator=boost::heap_clone_allocator,
1> allow_null_values=false
1> ]
1> c:program filesboostboost_1_49_0boostptr_containerdetailreversible_ptr_container.hpp(276) : see reference to class template instantiation 'boost::ptr_container_detail::reversible_ptr_container<Config,CloneAllocator>::null_clone_allocator<allow_null_values>' being compiled
1> with
1> [
1> Config=boost::ptr_container_detail::sequence_config<Object,std::vector<void *,std::allocator<void *>>>,
1> CloneAllocator=boost::heap_clone_allocator,
1> allow_null_values=false
1> ]
1> c:program filesboostboost_1_49_0boostptr_containerdetailreversible_ptr_container.hpp(275) : while compiling class template member function 'void boost::ptr_container_detail::reversible_ptr_container<Config,CloneAllocator>::null_policy_deallocate_clone(const Object *)'
1> with
1> [
1> Config=boost::ptr_container_detail::sequence_config<Object,std::vector<void *,std::allocator<void *>>>,
1> CloneAllocator=boost::heap_clone_allocator
1> ]
1> c:program filesboostboost_1_49_0boostptr_containerptr_sequence_adapter.hpp(132) : see reference to class template instantiation 'boost::ptr_container_detail::reversible_ptr_container<Config,CloneAllocator>' being compiled
1> with
1> [
1> Config=boost::ptr_container_detail::sequence_config<Object,std::vector<void *,std::allocator<void *>>>,
1> CloneAllocator=boost::heap_clone_allocator
1> ]
1> c:program filesboostboost_1_49_0boostptr_containerptr_vector.hpp(35) : see reference to class template instantiation 'boost::ptr_sequence_adapter<T,VoidPtrSeq,CloneAllocator>' being compiled
1> with
1> [
1> T=Object,
1> VoidPtrSeq=std::vector<void *,std::allocator<void *>>,
1> CloneAllocator=boost::heap_clone_allocator
1> ]
1> c:generaldevseniortankbattle3dtankbattle3dtankbattle3droom.h(28) : see reference to class template instantiation 'boost::ptr_vector<T>' being compiled
1> with
1> [
1> T=Object
1> ]
这是否意味着为了能够克隆东西,我必须扔掉抽象基类?
笔记:
- 在任何时候,程序中都不应该存在抽象基类的对象,但几乎所有内容都将被视为这样。
- 当我使克隆方法非虚拟,并为其提供实际返回某些内容的方法(提供一个构造函数/复制构造函数,这意味着它们可以存在,这违背了设计(,然后编译器抛出所有派生类都需要一个默认构造函数。 没有它们是设计使然。
编辑:我没有实现delete_clone()
(不知道这是明确要求的,我认为析构函数会做得很好(
class Object{
public :
ObjectType superType;
bool toBeRemoved;
virtual void performAction(int action, Object& source){}
virtual void updateObject(float duration){}
virtual ~Object(){}
virtual Object * new_clone(void)const = 0; // Object.h[36]
bool operator==(const Object& _other)const;
bool operator!=(const Object& _other)const;
};
你想要的是一个独立的(非成员(new_clone()
函数,编译器将优先于Boost clone_allocator.hpp
中的函数
class Object{
public :
ObjectType superType;
bool toBeRemoved;
virtual void performAction(int action, Object& source){}
virtual void updateObject(float duration){}
virtual ~Object(){}
bool operator==(const Object& _other)const;
bool operator!=(const Object& _other)const;
private:
virtual Object * do_clone() const = 0;
};
// in the same namespace as `class Object`:
// so it will get picked up instead of Boost's default
// implementation
inline
Object* new_clone( const Object& o)
{
return o.do_clone();
}
相关文章:
- 多态性和功能结合
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- C++boost序列化多态性问题
- 如何查找哪个类对象位于数组的特定索引上(多态性)
- 如何在多线程中正确使用unique_ptr进行多态性?
- 具有智能指针的多态性
- 在 C++ 中在堆栈上创建实例时如何保持多态性?
- 继承/多态性 - 我是否被迫使用"protected"变量?
- C++ 多态性在代码::块 17.12 中不起作用
- C++ 泛型和多态性:这种模式可行吗?
- 为什么我们实际上需要运行时多态性?
- 如何在这个简单的最小示例中实现多态性?
- 如何使用静态多态性在 int 和指针类型之间进行转换?
- 无法初始化已知大小的矢量指针,该大小不会因多态性而更改
- 运行时多态性和dynamic_cast需要澄清
- 如何调用指针类型的方法(禁用多态性)?
- 从基类调用函数的多态性
- 运行时多态性 - 箭头运算符访问了错误的成员?
- 为什么我在虚幻引擎中的多态性和接口方面遇到问题?