English 中文(简体)
c++ 继承等级的虚拟功能
原标题:c++ virtual functions in inheriting class
  • 时间:2012-04-20 20:37:55
  •  标签:
  • c++

我知道,在你想要宣布多变功能时,你必须宣布基类功能虚拟。

class Base
{
public:
    virtual void f();
};

我的问题是,你是否要求宣布继承的班级功能为虚拟功能,即使它预计儿童的行为与“淡化”一样?

class Child : public Base
{
public:
    void f();
};
最佳回答

无,你不需要重新宣布这一功能。

a) 基准类别中的“虚拟”栏目:

struct A
{
   void foo();          //not virtual
};
struct B : A
{
   virtual void foo();  //virtual
}
struct C : B
{
   void foo();          //virtual
}
问题回答

宣布(f)为“儿童虚拟”有助于阅读儿童的定义。 它是有用的文件。

一旦基类上下限,即标明为virtual。 所有其他压倒性做法都是如此。 虽然您不必将这一职能标记为virtual。 为了文件目的,我倾向于这样做。

As of the last part: even if it s expected that Child behaves as if it were "sealed"?, if you want to seal the class, you can actually do it in C++11 (this was not fully implementable in C++03 generically) by creating a seal class like so:

template <typename T>
class seal {
   seal() {}
   friend T;
};

之后继承您的密封等级(CRTP):

class Child : public Base, virtual seal<Child> {
// ...
};

The trick is that because of the use of virtual inheritance, the most derived type in the hierarchy must call the virtual base constructor (int this case seal<Child>), but that constructor is private in the templated class, and only available to Child through the friend declaration.

在C++中,你要么必须建立<条码>。 您希望密封的每类物品的类型,或采用不提供perfect密封的通用方法(可以更改)





相关问题
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?

热门标签