多态与复制构造函数 [英] Polymorphism with copy constructor
问题描述
这里是我使用的代码。我想知道我做的是正确和安全。通常它编译,我做的测试是成功的。但是因为这是我第一次使用 dynamic_cast
和 static_cast
,我想确定我didn不要错过任何东西。
Here is the code I use. I'd like to know if what I did is correct and safe. Normally it compiles and the tests I did are successful. But as it is the first time that I use dynamic_cast
and static_cast
, I'd like to be sure that I didn't miss anything.
您可以查看:
- 我不会忘记删除任何指针(我真的不明白我的克隆方法...)
- 如果我正确使用
dynamic_cast
- that I don't forget to delete any pointer (I don't really understand what my clone method does...)
- if I use correctly
dynamic_cast
andstatic_cast
- that there is not a more efficient of doing it
但我的主要担心是我正确实现 clone()方法
But my main concern is that I correctly implement the clone() method
很多!!!
感谢一些答案,我意识到我应该重新设计我的代码。所以我可以摆脱这段代码...
Thanks to some answer I realized that I should redesign my code. So I could get rid of this piece of code...
.hpp文件:
#include <vector>
/*!Base class that will never be instantiate. It provides a way to create
* GenericData with defferent type. */
class Data{
public:
/*!Constructor*/
Data(std::string name):name_(name){}
/*!Destructor, must be virtual*/
virtual ~Data(){};
/*!Method that allows a copy of the derived class*/
virtual Data* clone() const = 0;
protected:
std::string name_;//!< Name of the (Generic)Data
};
/*!Derived class that can contain any one value of any type and of name name_*/
template<typename Type>
class GenericData : public Data{
public:
/*!Constructor*/
GenericData(std::string name, Type t): Data(name), t_(t){}
/*!Destructor*/
~GenericData(){};
/*!Method that implements*/
GenericData<Type>* clone() const { return new GenericData<Type>(static_cast<GenericData<Type> const&>(*this) );}
/*!Returns the value of the data*/
Type const& get_val() const {return t_;}
private:
Type t_;//!< Value of the GenericData
};
/*!Contains a vector of Data*, can store diffrent GenericData*/
class Container{
public:
Container(){};
Container(Container const& c){
for(unsigned int i(0);i<c.data_.size();i++){
data_.push_back((c.data_[i])->clone());
}
}
~Container(){
for(unsigned int i(0);i<data_.size();i++){
delete data_[i];
data_[i] = NULL;
}
}
/*!Add one GenericData<Type> of value t and named name*/
template<typename Type>
void set(std::string name, Type const& t){data_.push_back(new GenericData<Type>(name,t));}
/*!Returns the value GenericData<Type>::t_ named name*/
template<typename Type>
Type get(std::string name) const;
private:
std::vector<Data*> data_;
};
template<typename Type>
void Container::set(std::string name, Type const& t){
data_.push_back(new GenericData<Type>(name,t));
}
template<typename Type>
Type Container::get(std::string name) const {
for(unsigned int i(0);i<data_.size();i++){
if(data_[i]->get_name()==name){
return dynamic_cast< GenericData<Type> *>(data_[i])->get_val();
}
}
std::cerr<<"Container : get(string name) : no data with name "<<name<<std::endl;
return 0;
}
推荐答案
GenericData<Type>* clone() const{
return new GenericData<Type>(static_cast<GenericData<Type> const&>(*this) );
}
在这里你不需要使用 static_cast
,因为 *的此类
已经是 const GenericData< Type>&
。
Here you don't need to use static_cast
because type of *this
is already const GenericData<Type>&
.
=================================== =============
================================================
if(data_[i]->get_name()==name){
return dynamic_cast< GenericData<Type> *>(data_[i])->get_val();
}
您需要检查 dynamic_cast
返回空指针。如果data_ [i]不是类型 GenericData< Type> *
(或者如果它不是从派生的某种类型的指针, GenericData< Type>
)
Here you need to check whether dynamic_cast
returns null pointer. null pointer will be returned if data_[i] is not of type GenericData<Type>*
(or if it's not a pointer to some type derived from GenericData<Type>
)
========================= ======================
您还需要定义 Container :: operator =
。如果你不定义它,编译器会为你生成它,并且向量< Data *>
可能被不正确地复制。
=============================================
You also need to define Container::operator=
. If you don't define it the compiler will generate it for you and vector<Data*>
can be improperly copied.
=================================== ==========
=============================================
我的建议是使用 std :: shared_ptr< Data>
( boost :: shared_ptr< Data>
如果使用C ++ 98)或 std :: unique_ptr<>
而不是 Data *
,以避免手动对象删除和相关的潜在内存泄漏。
Also my advice is to use std::shared_ptr<Data>
(of boost::shared_ptr<Data>
if you use C++98) or std::unique_ptr<>
instead of Data*
to avoid manual object deletion and related potential memory leaks.
这篇关于多态与复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!