如果指向基类,如何复制向量 [英] How to copy a vector if pointers to base class
问题描述
你好
我正在尝试为指向
a基类(这是抽象的)的指针向量实现一个复制构造函数
我的问题是我怎么知道我应该分配什么样的新类型?因为基础poiner可以具有多重含义。
i知道我可以使用dynamic_cast但是我真的不喜欢这个
解决方案而且我想知道我能做些什么更好
谢谢
hello
I''m trying to implement a copy constructor for a vector of pointers to
a base class (which is abstract)
My question is how would i know what kind of new type should i
allocate since the base poiner can have multipull meanings.
i know i can use dynamic_cast however i really don''t like this
solution and i was wondering is i can do something better
thanks
推荐答案
Alex Snast写道:
Alex Snast wrote:
hello
我正在尝试为一个指针向量实现一个复制构造函数
a基类(这是抽象的)
我的问题是如何我知道我应该分配什么样的新类型,因为基础poiner可以有多重含义。
i知道我可以使用dynamic_cast但是我真的不喜欢不喜欢这个
解决方案我想知道我能做些更好的事吗
hello
I''m trying to implement a copy constructor for a vector of pointers to
a base class (which is abstract)
My question is how would i know what kind of new type should i
allocate since the base poiner can have multipull meanings.
i know i can use dynamic_cast however i really don''t like this
solution and i was wondering is i can do something better
标准技巧是在基类中提供一个虚拟clone()成员。
然后你可以使用带有back_inserter的std :: transform复制向量
和这样的仿函数
Base * deep_copy(Base * ptr) {
return(ptr-> clone());
}
或其中一些指向成员函数的不可读指针粘合剂。
您还可以使用copy_ptr或clone_ptr的存档来查找具有深层复制语义的智能
指针。这些是非标准的,并且可能会使你的代码更难维护,因为他们可能不熟悉这些库的
。从好的方面来说,你不需要做任何事情,因为智能指针的复制构造函数将给作业带来一些向量。
复制那些向量。< br $> b $ b最佳
Kai-Uwe Bux
The standard trick is to provide a virtual clone() member in the base class.
You can then copy the vector by using std::transform with a back_inserter
and a functor like this
Base* deep_copy ( Base * ptr ) {
return( ptr->clone() );
}
or some of these unreadable pointer to member function binders.
You can also google the archives for copy_ptr or clone_ptr to find smart
pointers with deep copy semantics. Those are non-standard and might make
your code harder to maintain for others since they might not be familiar
with those libraries. On the plus side, you don''t need to do anything to
copy a vector of those since the copy constructor of the smart pointer will
to the job.
Best
Kai-Uwe Bux
Kai-Uwe Bux×?×a×?:
Kai-Uwe Bux ×?×a×?:
Alex Snast写道:
Alex Snast wrote:
hello
我正在尝试实现一个复制构造函数,用于指向
a基类的指针向量(这是抽象的)
我的问题是我怎么知道我应该分配什么样的新类型?因为基础poiner可以有多重含义。
i know i可以使用dynamic_cast然而我真的不喜欢这个
解决方案我想知道我能做些更好的事情
hello
I''m trying to implement a copy constructor for a vector of pointers to
a base class (which is abstract)
My question is how would i know what kind of new type should i
allocate since the base poiner can have multipull meanings.
i know i can use dynamic_cast however i really don''t like this
solution and i was wondering is i can do something better
标准技巧是在基类中提供虚拟clone()成员。
然后您可以复制通过使用带有back_inserter的std :: transform
和类似这样的仿函数的向量
Base * deep_copy(Base * ptr){
return(ptr-> clone());
}
或其中一些指向成员函数绑定器的不可读指针。
您还可以使用copy_ptr或clone_ptr的归档来查找具有深层复制语义的智能
指针。这些是非标准的,并且可能会使你的代码更难维护,因为他们可能不熟悉这些库的
。从好的方面来说,你不需要做任何事情,因为智能指针的复制构造函数将给作业带来一些向量。
复制那些向量。< br $>
最佳
Kai-Uwe Bux
The standard trick is to provide a virtual clone() member in the base class.
You can then copy the vector by using std::transform with a back_inserter
and a functor like this
Base* deep_copy ( Base * ptr ) {
return( ptr->clone() );
}
or some of these unreadable pointer to member function binders.
You can also google the archives for copy_ptr or clone_ptr to find smart
pointers with deep copy semantics. Those are non-standard and might make
your code harder to maintain for others since they might not be familiar
with those libraries. On the plus side, you don''t need to do anything to
copy a vector of those since the copy constructor of the smart pointer will
to the job.
Best
Kai-Uwe Bux
感谢您的帮助。工作只是发现即使我没有使用
std :: transform而是写了我自己的方法
Storerepository :: Storerepository(const Storerepository& rhs)
throw(OutOfMemory)
{
尝试{
for(const_repertory_iterator cit = rhs.repertory_.begin(); cit !=
rhs.repertory_.end(); ++ cit){
this-> repertory_.push_back((* cit) - > clone()) ;
}
}
catch(std :: bad_alloc){
抛出OutOfMemory();
}
}
再次感谢''
Thanks for the help. Worked just find even though i haven''t used
std::transform but rather wrote my own method
Storerepository::Storerepository(const Storerepository& rhs)
throw(OutOfMemory)
{
try {
for (const_repertory_iterator cit = rhs.repertory_.begin(); cit !=
rhs.repertory_.end(); ++cit) {
this->repertory_.push_back((*cit)->clone());
}
}
catch(std::bad_alloc){
throw OutOfMemory();
}
}
thanks again''
文章
< 84 ********************************** @ 25g2000hsx.g ooglegroups。 com>,
Alex Snast< as **** @ gmail.comwrote:
In article
<84**********************************@25g2000hsx.g ooglegroups.com>,
Alex Snast <as****@gmail.comwrote:
Kai-Uwe Bux ??·?
Kai-Uwe Bux ??·?
Alex Snast写道:
Alex Snast wrote:
?
?
hello
hello
?Y?
?Y?
我正在尝试为指向
a基类的指针向量实现一个复制构造函数(这是抽象的)
I''m trying to implement a copy constructor for a vector of pointers to
a base class (which is abstract)
?Y?
?Y?
我的问题是我怎么知道自从基数以来我应该分配什么样的新类型?b $ b poiner可以有多重意义。
My question is how would i know what kind of new type should i
allocate since the base poiner can have multipull meanings.
?Y?
?Y?
i知道我可以使用dynamic_cast但是我真的不喜欢这个
解决方案我是想知道我能做些更好的事吗
i know i can use dynamic_cast however i really don''t like this
solution and i was wondering is i can do something better
?
?
标准技巧是在基础
类中提供虚拟clone()成员。
你然后可以使用带有back_inserter
的std :: transform和像这样的
The standard trick is to provide a virtual clone() member in the base
class.
You can then copy the vector by using std::transform with a back_inserter
and a functor like this
的仿函数来复制向量?
?
Base * deep_copy(Base * ptr){
return(ptr-> clone());
Base* deep_copy ( Base * ptr ) {
return( ptr->clone() );
?YYY?
?
?YYY?
?
或其中一些指向成员函数绑定器的不可读指针。
or some of these unreadable pointer to member function binders.
?
?
您还可以使用copy_ptr或clone_ptr的存档来查找具有深层复制语义的智能
指针。这些是非标准的,并且可能会使你的代码更难维护,因为他们可能不熟悉这些库的
。从好的方面来说,你不需要做任何事情。
复制那些矢量,因为智能指针的复制构造函数将为工作提供
。
You can also google the archives for copy_ptr or clone_ptr to find smart
pointers with deep copy semantics. Those are non-standard and might make
your code harder to maintain for others since they might not be familiar
with those libraries. On the plus side, you don''t need to do anything to
copy a vector of those since the copy constructor of the smart pointer will
to the job.
?
?
?
?
Best
Best
?
?
Kai-Uwe Bux
Kai-Uwe Bux
感谢您的帮助。工作只是发现即使我没有使用
std :: transform而是写了我自己的方法
Thanks for the help. Worked just find even though i haven''t used
std::transform but rather wrote my own method
Storerepository :: Storerepository(const Storerepository& rhs)throw(OutOfMemory)
{
try {
for(const_repertory_iterator cit = rhs.repertory_.begin ();
cit!= rhs.repertory_.end(); ++ cit){
this-> repertory_.push_back((* cit) - > clone());
}
}
catch(std :: bad_alloc){
抛出OutOfMemory( );
}
}
Storerepository::Storerepository(const Storerepository& rhs) throw(OutOfMemory)
{
try {
for (const_repertory_iterator cit = rhs.repertory_.begin();
cit != rhs.repertory_.end(); ++cit) {
this->repertory_.push_back((*cit)->clone());
}
}
catch(std::bad_alloc){
throw OutOfMemory();
}
}
仅仅是为了纪录,Kai-Uwe推荐这样的东西:
transform(rhs.repertory_.begin(),rhs.repertory_.end(),
back_inserter(repertory_),& deep_copy);
Or:
transform(rhs.repertory_.begin(),rhs.repertory_.end(),
back_inserter(repertory_),mem_fun (& Base :: clone));
您可以将上面的内容转换为将bad_alloc转换为OutOfMemory,但是我没有看到
看不到打扰的理由......
Just for the record, Kai-Uwe was recommending something like this:
transform(rhs.repertory_.begin(), rhs.repertory_.end(),
back_inserter(repertory_), &deep_copy);
Or:
transform(rhs.repertory_.begin(), rhs.repertory_.end(),
back_inserter(repertory_), mem_fun(&Base::clone));
You can wrap the above to convert a bad_alloc into an OutOfMemory, but I
don''t see a reason to bother...
这篇关于如果指向基类,如何复制向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!