English 中文(简体)
超载超等级职能
原标题:Overloading a super class s function

在C++标准中是否有东西妨碍我超负荷超重履行超级班级职能?

从这个课堂开始:

class A {            // super class
    int x;

public:
    void foo (int y) {x = y;}  // original definition
};

class B : public A { // derived class
    int x2;

public:
    void foo (int y, int z) {x2 = y + z;}  // overloaded
};

I can call B::foo() easily:

    B b;
    b.foo (1, 2);  // [1]

但是,如果我试图打上<代码>A:foo(......

    B b;
    b.foo (12);    // [2]

...... 我发现一个汇编错误:

test.cpp: In function  void bar() :
test.cpp:18: error: no matching function for call to  B::foo(int) 
test.cpp:12: note: candidates are: void B::foo(int, int)

为了保证我没有一件事,我改变了<代码>的名称。 B功能,以便无超载:

class B : public A {
    int x2;

public:
    void stuff (int y, int z) {x2 = y + z;}  // unique name
};

现在,我可以使用第二个例子打电话A:foo(

这一标准吗? I m使用g++。

最佳回答

您需要使用<代码>定义内的一项声明。 B:

class B : public A {
public:
    using A::foo;          // allow A::foo to be found
    void foo(int, int);
    // etc.
};

如果没有使用声明,汇编者发现B:foo ,在名称查询期间,实际上没有为名称相同的其他实体寻找基级,例如A:foo

问题回答

请不要凌驾于<条码>A:foo(int) 执行,而是将<条码>:foo<>/code>改为(int,int)而不是(int)。 正如James McNellis提到using A:foo;的声明使A成为可能。





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

热门标签