继承自std :: vector? [英] Inheriting from std::vector?
问题描述
一位同事有这样的代码:
class CMyObject {
//一群会员功能
}
类CMyObjectList:public std :: vector< CMyObject>
{
//一堆成员函数
}
我需要继承他的对象和列表的功能以及添加
我自己的一些:
class CMyPersonalObject:public CMyObject
{
//更多东西
}
类CMyPersonalObjectList:public CMyObjectList
{
//更多东西
}
但是如果我将CMyPersonalObjectList传递给一个函数,然后尝试将CMyPersonalObject分配给该类型的局部变量,我得到一个编译器
错误告诉我我无法将CMyObject转换为CMyPersonalObject ...
编译器认为我传入的列表是由父对象组成的。
发生了什么事,我该如何解决?
谢谢,
B
Hi,
A colleague has some code like this:
class CMyObject {
// Bunch of Member functions
}
class CMyObjectList: public std::vector<CMyObject>
{
// Bunch of member functions
}
I need to inherit the functionality of his object and list as well as add
some of my own:
class CMyPersonalObject: public CMyObject
{
// More stuff
}
class CMyPersonalObjectList: public CMyObjectList
{
// More stuff
}
But if I pass in CMyPersonalObjectList to a function, and then try to assign
the CMyPersonalObject to a local variable of that type, I get a compiler
error telling me that I cannot convert CMyObject to CMyPersonalObject... the
compiler thinks the list I passed in is made of the parent objects.
What is going on and how do I fix it?
Thanks,
B
推荐答案
BCC写道:
BCC wrote:
但是如果我将CMyPersonalObjectList传递给一个函数,然后尝试将CMyPersonalObject分配给这个类型的局部变量,我得到一个编译器
错误告诉我我无法将CMyObject转换为CMyPersonalObject ...
编译器认为我传入的列表是由父对象组成的。
发生了什么以及如何解决?
But if I pass in CMyPersonalObjectList to a function, and then try to assign
the CMyPersonalObject to a local variable of that type, I get a compiler
error telling me that I cannot convert CMyObject to CMyPersonalObject... the
compiler thinks the list I passed in is made of the parent objects.
What is going on and how do I fix it?
需要查看分配的代码。
但是,由于std :: vector没有虚拟析构函数,因此
不应该被用作基类。这不是你实际问题的原因。
Would need to see the code where the assignment is made.
However, as std::vector doesn''t have a virtual destructor it
shouldn''t really be being used as a base class. That isn''t
your actual problem though.
2003年10月23日星期四23:47:14 GMT, " BCC" <峰; br *** @ akanta.com>写道:
On Thu, 23 Oct 2003 23:47:14 GMT, "BCC" <br***@akanta.com> wrote:
一位同事有这样的代码:
类CMyObject {
//一群成员函数
}
缺少分号。
类CMyObjectList:public std :: vector< CMyObject>
{
//一串成员函数>}
同上。
如果没有进一步的信息,这种继承有点可疑。
我可以从std :: string继承,但不是std :: vector。
我需要继承他的对象和列表的功能以及添加
类CMyPersonalObject:public CMyObject
{
//更多东西
}
类CMyPersonalObjectList:public CMyObjectList
{
//更多东西
}
但是如果我将CMyPersonalObjectList传递给一个函数,然后尝试将CMyPersonalObject分配给一个函数这种类型的局部变量,
没有重新CMyPersonalObjectList和CMyPersonalObject之间的关系
你所展示的内容,所以上面似乎毫无意义。
我得到一个编译器
错误告诉我我无法将CMyObject转换为CMyPersonalObject ...
编译器是对的,除非你提供转换,否则你不能这样做。
编译器认为我传入的列表是由父对象。
你到底在哪里得到那个荒谬的想法?编者不会想。
发生什么事情
见例如< url:http://news.google.com/>。
我该如何解决?
A colleague has some code like this:
class CMyObject {
// Bunch of Member functions
}
Missing semicolon.
class CMyObjectList: public std::vector<CMyObject>
{
// Bunch of member functions
}
Ditto.
Without further information this inheritance is a little suspicious.
I can thing of good reasons to inherit from std::string, but not std::vector.
I need to inherit the functionality of his object and list as well as add
some of my own:
class CMyPersonalObject: public CMyObject
{
// More stuff
}
class CMyPersonalObjectList: public CMyObjectList
{
// More stuff
}
But if I pass in CMyPersonalObjectList to a function, and then try to assign
the CMyPersonalObject to a local variable of that type,
There is no relationship between CMyPersonalObjectList and CMyPersonalObject
in what you have shown, so the above seems meaningless.
I get a compiler
error telling me that I cannot convert CMyObject to CMyPersonalObject...
The compiler is right, you cannot do that unless you provide a conversion.
the compiler thinks the list I passed in is made of the parent objects.
Where on earth did you get that absurd idea? Compilers don''t think.
What is going on
See e.g. <url: http://news.google.com/>.
and how do I fix it?
也许关注Michael Moore的建议?
Perhaps follow the advice of Michael Moore?
2003年10月23日星期四23:47:14 GMT,BCC< br *** @ akanta.com>写道:
On Thu, 23 Oct 2003 23:47:14 GMT, BCC <br***@akanta.com> wrote:
一位同事有这样的代码:
类CMyObject {
//束会员职能
}
类CMyObjectList:public std :: vector< CMyObject>
{
//一群成员函数
} >
我需要继承他的对象和列表的功能以及添加我自己的一些:
类CMyPersonalObject:public CMyObject
{
//更多东西
}
类CMyPersonalObjectList:public CMyObjectList
{
//更多东西
}
但是如果我将CMyPersonalObjectList传递给函数,然后尝试将CMyPersonalObject分配给该类型的局部变量,我得到编译器错误告诉我无法将CMyObject转换为CMyPersonalObject ...
编译器认为我传入的列表是由父对象组成的。
发生了什么以及如何解决它?
<谢谢,
B
Hi,
A colleague has some code like this:
class CMyObject {
// Bunch of Member functions
}
class CMyObjectList: public std::vector<CMyObject>
{
// Bunch of member functions
}
I need to inherit the functionality of his object and list as well as add
some of my own:
class CMyPersonalObject: public CMyObject
{
// More stuff
}
class CMyPersonalObjectList: public CMyObjectList
{
// More stuff
}
But if I pass in CMyPersonalObjectList to a function, and then try to
assign
the CMyPersonalObject to a local variable of that type, I get a compiler
error telling me that I cannot convert CMyObject to CMyPersonalObject...
the
compiler thinks the list I passed in is made of the parent objects.
What is going on and how do I fix it?
Thanks,
B
std :: vector是一个模板。 CMyObjectList也应该是一个模板,如果你想拥有个人对象列表,最低价格为
。
template< class T> CObjectList:public std :: vector< T> {
更多
};
typedef CObjectList< CMyPersonalObject> CMyPersonalObjectList;
-
grzegorz
std::vector is a template . CMyObjectList should also be a template, at
least if you want to have personalobject list.
template <class T> CObjectList : public std::vector<T> {
some more
};
typedef CObjectList<CMyPersonalObject> CMyPersonalObjectList;
--
grzegorz
这篇关于继承自std :: vector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!