加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 无法识别的继承功能

发布时间:2020-12-16 10:26:18 所属栏目:百科 来源:网络整理
导读:在Visual C 2010上运行此C源: class B{public: virtual void f(int a){} virtual void f(){}};class A:public B{public: virtual void f(int a){}};int main(){A a;a.f();return 0;} 导致以下错误: IntelliSense:函数调用中的参数太少 换句话说,似乎void
在Visual C 2010上运行此C源:

class B{
public:
    virtual void f(int a){}
    virtual void f(){}
};
class A:public B{
public:
    virtual void f(int a){}
};
int main(){
A a;
a.f();
return 0;
}

导致以下错误:

IntelliSense:函数调用中的参数太少

换句话说,似乎void f()没有继承?

问题是什么?

解决方法

it seems that void f() didn’t inherited?

A类中的名称f在B中隐藏名称f.您仍然可以通过这种方式从B访问foo()

A a;
a.B::f();

其他选项是在A的范围内重新声明B的函数foo:

class A : public B{
public:

    virtual void f(int a) {}
    using B::foo;
};

C标准n3337§10.2成员名称查找

1) Member name lookup determines the meaning of a name (id-expression)
in a class scope (3.3.7). Name lookup can result in an ambiguity,in
which case the program is ill-formed. For an id-expression,name
lookup begins in the class scope of this; for a qualified-id,name
lookup begins in the scope of the nested- name-specifier. Name lookup
takes place before access control (3.4,Clause 11).

2) The following steps define the result of name lookup for a member
name f in a class scope C.

3) The lookup set for f in C,called S(f,C),consists of two
component sets: the declaration set,a set of members named f; and the
subobject set,a set of subobjects where declarations of these members
(possibly including using-declarations) were found. In the declaration
set,using-declarations are replaced by the members they designate,
and type declarations (including injected-class-names) are replaced by
the types they designate. S(f,C) is calculated as follows:

4) If C contains a declaration of the name f,the declaration set
contains every declaration of f declared in C that satisfies the
requirements of the language construct in which the lookup occurs. [
Note: Looking up a name in an elaborated-type-specifier (3.4.4) or
base-specifier (Clause 10),for instance,ignores all non- type
declarations,while looking up a name in a nested-name-specifier
(3.4.3) ignores function,variable,and enumerator declarations. As
another example,looking up a name in a using-declaration (7.3.3)
includes the declaration of a class or enumeration that would
ordinarily be hidden by another declaration of that name in the same
scope. — end note ] If the resulting declaration set is not empty,the
subobject set contains C itself,and calculation is complete.

5) Otherwise (i.e.,C does not contain a declaration of f or the resulting declaration set is empty),S(f,C) is initially empty. If C has base classes,calculate the lookup set for f in each direct base class subobject Bi,and merge each such lookup set S(f,Bi ) in turn into S(f,C).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读