- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
对于我当前的项目,我使用 boost::ptr_vector
以多态方式保存 Objects
,并且一切正常,直到我的一个构建 VS2010 抛出它可以不克隆 object
所以我去了,查找克隆,并按照 boost
的要求实现了 new_clone()
方法,并遵循 c++ FAQ使其成为纯虚拟
,但现在 VS2010 向我抛出这个 shanagin 说它不能在抽象类上使用克隆。
1>c:\program files\boost\boost_1_49_0\boost\ptr_container\clone_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 files\boost\boost_1_49_0\boost\ptr_container\clone_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 files\boost\boost_1_49_0\boost\ptr_container\detail\reversible_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 files\boost\boost_1_49_0\boost\ptr_container\detail\reversible_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 files\boost\boost_1_49_0\boost\ptr_container\detail\reversible_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 files\boost\boost_1_49_0\boost\ptr_container\detail\reversible_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 files\boost\boost_1_49_0\boost\ptr_container\ptr_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 files\boost\boost_1_49_0\boost\ptr_container\ptr_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:\general\dev\senior\tankbattle3d\tankbattle3d\tankbattle3d\room.h(28) : see reference to class template instantiation 'boost::ptr_vector<T>' being compiled
1> with
1> [
1> T=Object
1> ]
这是否意味着为了让 boost 能够克隆我必须丢弃抽象基类的东西?
注意事项:
编辑:我没有实现 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;
};
最佳答案
你要的是独立的(非成员(member))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();
}
关于c++ - boost::ptr_container 中的克隆分配器和多态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10475509/
我有一个 map我需要插入和删除 Foo * 的地方.用法看起来像 map mapping; while( a long time) { // make ne
我想知道这是做什么的: std::basic_string, std::allocator>:: basic_string, std::allocator> (&myText, "hello worl
是否可以在 C++ 中创建一个像这样简单工作的自定义分配器: { // Limit memory to 1024 KB ScopedMemoryPool memoryPool(1024
我正在使用提到的 STL 分配器 here . 我所做的唯一更改是我从一个名为 Object 的基类继承,并且我使用基类的 new 和 delete 函数进行分配。 class MyAlloc
我有一段代码可以创建数千个对象,并将它们附加到一个 vector 中。下面的代码只是一个正在做的事情的例子,尽管构造函数有一些参数,而for实际上并没有那个条件,但它起到了表明它运行了数千次的目的。
这里有两个问题。首先,如果我需要在 Clone 之前创建 b2BlockAllocator 然后在克隆之后删除(在哪里?)? Xcode 分析工具未显示 C++ 泄漏... b2FixtureDef
我想创建一个不可复制的分配器(在 C++14 中),它只分配一个 std::vector 可以使用的固定内存块。我想防止分配器(以及 vector )被复制,以防止用户意外分配内存。分配器仅用于 st
我在 http://msdn.microsoft.com/en-us/library/ee292117.aspx 上看到和 http://msdn.microsoft.com/en-us/librar
我想用更健壮的分配器替换标准分配器(C++ 标准只需要对 vector::resize 进行溢出检查)。许多库提供的各种 C++ 分配器在进行负面 self 测试时会一败涂地。 我可以使用更强大的分配
我的 STL 容器中的内存使用预计是不稳定的——也就是说它会经常收缩和增长。我正在考虑通过为 STL 容器类型声明指定一个分配器来解决这个问题。我知道矿池分配器旨在处理这种情况,但我担心的是波动性将超
我有一个大量使用 STL 容器和字符串的大型(>250 个文件)库的源代码。我需要在有限堆的嵌入式环境中运行它,所以我想确保这个库本身的堆使用受到限制。 显而易见的解决方案是创建一个分配器,但修改整个
我想知道有一个符合 C++ 标准的库是否可行 allocator使用位于堆栈中的(固定大小的)缓冲区。 不知何故,这个问题似乎还没有在 SO 上这样问过,尽管它可能已经在其他地方得到了隐含的回答。 所
我观察到我的 MSVC10 副本附带的容器似乎允许基于状态的分配器,并编写了一个简单的池分配器,为特定类型分配池。 然而,我发现如果_ITERATOR_DEBUG_LEVEL != 0 MSVC 向量
据我所知,当 vector 空间不足时,分配器用于创建新空间。但是,我想创建一个自定义调整大小策略,该策略将移除底部 25% 的元素并始终保持相同的大小。这是为了构建一个空间有限的缓存。 有没有我可以
我目前正在尝试使用 Microsoft Visual Studio 2012 编译一个相当大的项目。我发现它在旧版本上编译得很好,但是对于这个版本,我在 std::list 的任何地方都会出错仅与一个
因此,在所提供代码的下一行,我有 IntelliSense 警告:“没有可用的成员”。怎么了?在正常情况下,似乎有选项,如“分配”、“解除分配”等。 namespace MyLib { tem
我正在尝试在 Microsoft visual studio 2013 on C++ 上编译为 linux 编写的程序。 声明 sdesc_t *ret = _malloc(sizeof(sdesc_
由于我工作的政策,我无法使用高于 1.33.1 的 Boost 版本,也无法使用高于 4.1.2 的 GCC 版本。是的,这是垃圾,但我对此无能为力。 Boost 1.33.1 不包含进程间库。 也就
我正在为 T 类型的数组实现资源分配克隆操作。直接的实现使用 new T[sz],然后是从源到新数组的 std::copy 调用。它遍历内存两次。 我想分配原始内存然后使用 std::uninitia
我们有一个库,它通过 extern "C" 提供 C 接口(interface),并从 C 代码中使用,但为了方便起见,它内部使用了 STL 容器和一些 C++ 功能,如 RAII。 现在有一个新的要
我是一名优秀的程序员,十分优秀!