就连我都觉得这个问题很愚蠢。但我有一点经验。
我有一个基类,它有这样的方法:
class A{ virtual void func(int)=0 };和继承的类
class B :public A
{
//how should i write?
//a
virtual void func() { implementation...}
//b
void func() {implementation }
//my target is to redefine a function of ansestor
//i worry that variant b can cover ansestor function and i will not redefine it
//but what if i don't want that the function that was virtual in ansestor, will be virtual in subclass?
i'm confused
}我不知道该怎么做。如果我不需要这个虚函数complete
发布于 2011-05-26 06:32:55
你会问,“如果我不想让在ansestor中是虚拟的函数,在子类中是虚拟的,该怎么办?”
抱歉,但是在基类中声明为virtual的每个函数在所有派生类中也是虚拟的。
实际上,在派生类声明中是否使用virtual关键字并不重要。选项a和b是相同的--在这两种情况下,B::func都是虚拟的。
发布于 2011-05-26 06:32:23
我建议您编写两个小程序,每个实现一个,以确定哪个适合您的需求。
发布于 2011-05-26 06:35:10
在C++中,函数签名由函数名和函数参数组成。在一个类中,不能有两个具有相同签名的函数。因此,您的第二个(非虚)函数声明将生成编译器错误。
简而言之:
virtual void func() { //implementation}和
void func() { //implementation }具有相同的签名,不能在同一个类中声明。
https://stackoverflow.com/questions/6131704
复制相似问题