gpt4 book ai didi

C++虚函数重新实现

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:43 26 4
gpt4 key购买 nike

我什至认为这个问题很愚蠢。但我有一点经验。

我有一个基类有这样的方法:

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
}

我不知道该怎么做。如果我不需要这个虚函数完成

最佳答案

你问,“如果我不希望在 ansestor 中是虚函数,在子类中也是虚函数怎么办?”

抱歉,在基类中声明为virtual 的每个函数在所有派生类中也是virtual。

在派生类声明中是否使用 virtual 关键字实际上并不重要。选项 a 和 b 是相同的——在这两种情况下 B::func 都是虚拟的。

关于C++虚函数重新实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6131704/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com