English 中文(简体)
我是否还需要对子类方法作虚拟说明?
原标题:Do I need to specify virtual on the sub-classes methods as well?

也许在SO问题上已经提出过,但我无法找到一个类似的问题。

考虑以下等级:

class BritneySpears
{
  public:

    virtual ~BritneySpears();
};

class Daughter1 : public BritneySpears
{
  public:

    virtual ~Daughter1(); // Virtual specifier
};

class Daughter2 : public BritneySpears
{
  public:

    ~Daughter2(); // No virtual specifier
};

<代码>Dumberer1和>>>>Dault.2之间有差别。 类别?

具体指明/未具体指明virtual在子流放器/方法上有何后果?

最佳回答

您在技术上无需具体说明virtual。 如果基数方法是虚拟的,C++将自动使对等方法凌驾于virtual

但是,您应当打上virtual。 该方法毕竟是virtual,使你的代码更加清晰,更易于其他开发商采用。

问题回答

你不需要,但打上标识可使你的法典更加明确。

Note: if your base class has a virtual destructor, then your destructor is automatically virtual. You might need an explicit destructor for other reasons, but there s no need to redeclare a destructor simply to make sure it is virtual. No matter whether you declare it with the virtual keyword, declare it without the virtual keyword, or don t declare it at all, it s still virtual.

不管你是否在子女类别中具体提到这种方法,都自动采用衍生方法。

其主要后果是,如果不具体说明儿童的虚拟情况,就更难以从儿童类别的定义中看出这种方法事实上是虚拟的。 为此原因,我总是在家长和子女班级都注明虚拟。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签