English 中文(简体)
C++ 继承和职能高于一切
原标题:C++ inheritance and function overriding

在C++,如果其原型(参数、类型和完整性)的不同,那么基类成员的职能将不受同一名称的衍生类别职能的约束? 我怀疑这一令人sil笑皆是的问题,因为许多网站都说,功能原型应当如此,才能做到这一点;但为什么要编纂以下代码? 我认为,这是一个非常简单的继承案例。

#include <iostream>
using std::cout;
using std::endl;

class A {};
class B {};

class X
{
public:
    void spray(A&)
    {
        cout << "Class A" << endl;
    }
};

class Y : public X
{
public:
    void spray(B&)
    {
        cout << "Class B" << endl;
    }
};

int main()
{
    A a;
    B b;
    Y y;

    y.spray(a);
    y.spray(b);

    return 0;
}

海湾合作委员会投掷石块

error: no matching function for call to `Y::spray(A&) 
note: candidates are: void Y::spray(B&)
最佳回答

用来描述这一术语的术语是“交易”,而不是“过度开采”。 某类衍生物的成员在缺席时,将无法使用同一名称的基类任何成员,不论他们是否拥有相同的签名。 如果你想要进入基类成员,你可以将其带入衍生类别,并附上<>使用的声明。 在此情况下,在<代码>Y上添加以下内容:

using X::spray;
问题回答

That s so called hiding : Y::spray hides X::spray. Add using directive:

class Y : public X
{
public:
   using X::spray;
   // ...
};

班级为范围,班级范围为母。 你的行为与其他封杀范围(名称、区块)完全相同。

确实发生的情况是,当对姓名定义进行点名搜查时,在目前的名称空间进行查询,然后在造名空间进行查询,直到找到一个定义;然后进行搜查(在不考虑以依赖名进行辩词引起的复杂情况的情况下进行查询——其中一部分规则允许使用以其中一种理由的名义界定的职能)。





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

热门标签