继承矢量 [英] Inheriting a vector
问题描述
我想要一个类似于类的向量,带有一些额外的功能
(化妆品一个)。所以我可以继承一个向量类来添加额外的
函数,例如,
CorresVector:public vector< Corres> {
public:
void addCorres(Corres& c); //它只做push_back
函数。
}
或者我需要做一个组合来做(即存储向量内部
Corresvector&授权必要的功能)。我想知道
,在这种情况下(继承)是否允许容器,即
当CorresVector被破坏时容器将被释放(即
vector有一个被调用的虚拟析构函数?)
abir
< blockquote>
toton写道:
我想要一个类似于类的向量功能性
(化妆品一件)。所以我可以继承一个向量类来添加额外的
函数,例如,
CorresVector:public vector< Corres> {
public:
void addCorres(Corres& c); //它只做push_back
函数。
}
或者我需要做一个组合来做(即存储向量内部
Corresvector&授权必要的功能)。我想知道
,在这种情况下(继承)是否允许容器,即
当CorresVector被破坏时容器将被释放(即
vector有一个虚拟析构函数被调用?)
否,vector没有虚析构函数。它没有任何
虚拟功能。你可以从一个向量继承但你不能这样做
多态。
toton写道:
我想要一个带有一些额外功能的类矢量
(化妆品一个)。所以我可以继承一个向量类来添加额外的
函数,例如,
CorresVector:public vector< Corres> {
public:
void addCorres(Corres& c); //它只做push_back
函数。
}
或者我需要做一个组合来做(即存储向量内部
Corresvector&授权必要的功能)。我想知道
,在这种情况下(继承)是否允许容器,即
当CorresVector被破坏时容器将被释放(即
vector有一个虚拟的析构函数被调用吗?)
abir
你好,abir,
std :: vector没有虚拟析构函数。
但如果你通过指针删除CorresVector它只需要一个
到基类。
关于它是否是正确的问题方法,使用谷歌和你
会发现很多不同的意见(参见例如
http://www.thescripts.com/forum/thread63869.html) 。
干杯,
弗兰克
Frank写道:
toton写道:
我想要一个带有一些附加功能的类矢量
(化妆品一)。所以我可以继承一个向量类来添加额外的
函数,例如,
CorresVector:public vector< Corres> {
public:
void addCorres(Corres& c); //它只做push_back
函数。
}
或者我需要做一个组合来做(即存储向量内部
Corresvector&授权必要的功能)。我想知道
,在这种情况下(继承)是否允许容器,即
当CorresVector被破坏时容器将被释放(即
vector有一个虚拟的析构函数被调用?)
abir
你好,
std :: vector没有虚拟析构函数。
但如果你通过指针删除CorresVector它只需要一个
到基类。
关于它是否是正确的问题方法,使用谷歌和你
会发现很多不同的意见(参见例如
http://www.thescripts.com/forum/thread63869.html)。
我有一个存储CorresVector的Session类,而不是指针或
引用。因此,Session类析构函数将自动调用
CorresVector。因此,没有变形行为。我希望因此不会有任何内存泄漏
。
否则委托包括容器在内的很多方法都很痛苦
traits&迭代器。
宗教绝对是一个问题,如果我的班级与向量的差异很大,或者
,可以考虑稍后委托(或
受保护的继承)任何其他误用问题来了(我知道Java Stack继承自
Vector!)。
干杯,
Frank
Hi,
I want to have a vector like class with some additional functionality
(cosmetic one). So can I inherit a vector class to add the addition
function like,
CorresVector : public vector<Corres>{
public:
void addCorres(Corres& c); //it do little more than push_back
function.
}
Or I need to do a composition to do it (i.e storing the vector inside
Corresvector & delegating the necessary functionality). I want to know
that whether in this case (inheriting) is allowed for containers, i.e
will the container will get freed when CorresVector gets destoryed (i.e
vector has a virtual destructor which gets called?)
abir
toton wrote:Hi,
I want to have a vector like class with some additional functionality
(cosmetic one). So can I inherit a vector class to add the addition
function like,
CorresVector : public vector<Corres>{
public:
void addCorres(Corres& c); //it do little more than push_back
function.
}
Or I need to do a composition to do it (i.e storing the vector inside
Corresvector & delegating the necessary functionality). I want to know
that whether in this case (inheriting) is allowed for containers, i.e
will the container will get freed when CorresVector gets destoryed (i.e
vector has a virtual destructor which gets called?)No, vector does not have a virtual destructor. It doesn''t have any
virtual functions. You can inherit from a vector but you can''t do so
polymorphically.
toton wrote:
Hi,
I want to have a vector like class with some additional functionality
(cosmetic one). So can I inherit a vector class to add the addition
function like,
CorresVector : public vector<Corres>{
public:
void addCorres(Corres& c); //it do little more than push_back
function.
}
Or I need to do a composition to do it (i.e storing the vector inside
Corresvector & delegating the necessary functionality). I want to know
that whether in this case (inheriting) is allowed for containers, i.e
will the container will get freed when CorresVector gets destoryed (i.e
vector has a virtual destructor which gets called?)
abirHi abir,
std::vector does not have a virtual destructor.
But it would only need one, if you delete a CorresVector through a pointer
to a base class.
As for the question whether it is the "right" approach, use google and you
will find a lot of different opinions (see e.g.
http://www.thescripts.com/forum/thread63869.html).
Cheers,
Frank
Frank wrote:toton wrote:
Hi,
I want to have a vector like class with some additional functionality
(cosmetic one). So can I inherit a vector class to add the addition
function like,
CorresVector : public vector<Corres>{
public:
void addCorres(Corres& c); //it do little more than push_back
function.
}
Or I need to do a composition to do it (i.e storing the vector inside
Corresvector & delegating the necessary functionality). I want to know
that whether in this case (inheriting) is allowed for containers, i.e
will the container will get freed when CorresVector gets destoryed (i.e
vector has a virtual destructor which gets called?)
abir
Hi abir,
std::vector does not have a virtual destructor.
But it would only need one, if you delete a CorresVector through a pointer
to a base class.
As for the question whether it is the "right" approach, use google and you
will find a lot of different opinions (see e.g.
http://www.thescripts.com/forum/thread63869.html).I have a Session class which stores the CorresVector, not a pointer or
reference. and Session class destructor will thus automatically call
CorresVector. Hence there is no ploymorphic behavior. I hope there will
not be any memory leak therefore.
Otherwise it is pain to delegate a lot of method including container
traits & iterators.
Religion is definitely a question, may think later of delegating (or
protected inheritence) if my class sufficiently differs from vector or
any other misuse question comes (I know Java Stack is inherited from
Vector ! ).Cheers,
Frank
这篇关于继承矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!