调用从抽象类派生的类的方法 [C++]
如果这是一个非常基本的问题,你必须原谅我;我已经很长时间没有使用过 C++ 了,所以我已经忘记了它是如何工作的。
无论如何,我有一个基类和几个像这样的派生类(超级过于简单,但要点是相同的):
class Base
{
public:
Base () { }
int SomeFunction (int x, int y); // abstract definition
};
class Derived1 : public Base
{
public:
Derived1() : Base() { }
int SomeFunction (int x, int y)
{
// actual implementation
return 4;
}
};
class Derived2 : public Base
{
public:
Derived2() : Base() { }
int SomeFunction (int x, int y)
{
// actual implementation
return 7;
}
};
稍后在 main
中,我有一个 Base
列表对象:
Base *baseobjects[10];
稍后我用 Derived1
和 Derived2
的实例填充该数组。这适用于 baseobjects[i] = &newDerived1
(其中 newDerived1
是 Derived1
类的实例)。没关系。
我似乎无法弄清楚如何稍后迭代 baseobjects
数组并在列表中的每个实例上调用 SomeFunction
而无需明确知道哪个派生类我正在使用。我已经在 C# 中完成了此操作,并且工作正常,但显然我的 C++ 语法已关闭:
int result = baseobjects[i]->SomeFunction(a, b);
当我尝试编译时,这给了我一个 LNK2019
错误,显然是因为它正在查看 Base< /code> 实现类,但它不存在。我假设我必须使用一些指针技巧来让它查看正确的派生方法,但我尝试过的任何方法都不起作用。有什么建议吗?
You'll have to forgive me if this is a really basic question; I haven't used C++ this much in a long time so I've forgotten how a lot of it works.
Anyway, I have a base class and a couple derived classes like this (super oversimplified, but the gist is the same):
class Base
{
public:
Base () { }
int SomeFunction (int x, int y); // abstract definition
};
class Derived1 : public Base
{
public:
Derived1() : Base() { }
int SomeFunction (int x, int y)
{
// actual implementation
return 4;
}
};
class Derived2 : public Base
{
public:
Derived2() : Base() { }
int SomeFunction (int x, int y)
{
// actual implementation
return 7;
}
};
Later on in main
I have a list of Base
objects:
Base *baseobjects[10];
Later I fill that array with instances of Derived1
and Derived2
. That works with baseobjects[i] = &newDerived1
(where newDerived1
is an instance of the Derived1
class). That's all fine.
What I can't seem to figure out is how to later iterate through the baseobjects
array and call SomeFunction
on every instance in the list without explicitly knowing which derived class I'm using. I've done this in C# and it works fine, but apparently my C++ syntax is off:
int result = baseobjects[i]->SomeFunction(a, b);
That gives me a LNK2019
error when I try to compile, apparently because it's looking at the Base
class for the implementation and it isn't there. I'm assuming I have to use some pointer tricks to get it to look at the proper derived method, but nothing I've tried yet has worked. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的方法应声明为
虚拟
。就您而言,可能是纯虚拟
。请注意,虽然这不是绝对必需的,但您也可以声明一个虚拟析构函数。如果您通过基类的指针删除派生实例,请执行此操作。
编辑:
另外,关于您发布的链接错误:
要么您忘记了
= 0
,要么您从某个地方调用Base::SomeFunction()
。正如 Thomas Edleson 指出的,
= 0
并不意味着您的函数没有实现:它可以有一个实现,但只需要派生类(重新)实现它不抽象。如果您对此主题感兴趣,我建议您阅读这篇文章。
Your method should be declared
virtual
. And in your case, probably purevirtual
.Note that, while this is not absolutely required, you might as well declare a
virtual
destructor. Do it if you ever delete a derived instance trough a pointer of the base class.Edit:
Also, regarding the link error you posted:
Either you forgot the
= 0
, either you are callingBase::SomeFunction()
from somewhere.As Thomas Edleson points out,
= 0
does not mean that your function has no implementation: it can have one, but it only requires the derived classes to (re)implement it to not being abstract.If you are interested in this topic, I suggest you read this post.
如果你想重写一个方法,它必须是虚拟的。
第二件事是你的派生类没有扩展你的基类。
If you want to override a method, it must be virtual.
Seccond thing is that your derivated classes did not extends of your base-class.
您必须声明成员函数 SomeFunction()
因此 Base 的声明应如下所示:
您可以在派生类中省略 virtual 关键字。
You have to declare the member function SomeFunction()
So the declaration for Base should look like this:
You can omit the virtual keyword in the derived classes.
为了稍微详细说明 ereOn 的答案,您确实需要声明您的基类函数(即:您的抽象定义),以便您的派生类能够覆盖它。但他没有澄清的是,除此之外,您发布的内容很好(也就是说,对您发布的函数的调用将无需任何修改即可工作)。
另请注意,如果您需要纯抽象定义,则应将
= 0
附加到函数声明中,如下所示:这可以让编译器知道从
Base
派生的类必须提供自己的SomeFunction
实现。To just slightly elaborate on ereOn's answer, you do need to declare you base class function (ie: your abstract definition) in order for your derived classes to be able to override it. What he didn't clarify though is that other than that, what you've posted is fine (that is, the calls to the function you posted will then work without any modification).
Note also, that if you're after a pure abstract definition, you should append
= 0
to your function declaration as follows:This lets the compiler know that classes derived from
Base
must supply their own implementation ofSomeFunction
.