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

c – 使用声明(派生类)

发布时间:2020-12-16 03:37:26 所属栏目:百科 来源:网络整理
导读:struct B1{ int d; void fb(){};};struct B2 : B1{ using B1::d; using B1::fb; int d; // why this gives error? void fb(){} // and this does not?};int main(){} 是因为,B1 :: fb()被视为B1 :: fb(B1 *)和B2 :: fb()被视为B2 :: fb(B2 *)?也就是说,隐含
struct B1{
  int d;
  void fb(){};
};

struct B2 : B1{
  using B1::d;
  using B1::fb;

  int d;               // why this gives error?
  void fb(){}          // and this does not?
};

int main(){}

是因为,B1 :: fb()被视为B1 :: fb(B1 *)和B2 :: fb()被视为B2 :: fb(B2 *)?也就是说,隐含参数,有助于区分这些吗?

$13.3.1/4-

For nonconversion functions introduced
by a using-declaration into a derived
class,the function is considered to
be a member of the derived class for
the purpose of defining the type of
the implicit object parameter.

解决方法

C标准(C03§7.3.3/ 12)解释:

When a using-declaration brings names from a base class into a derived class scope,member functions in the derived class override and/or hide member functions with the same name and parameter types in a base class (rather than conflicting).

在您的示例中,B2 :: fb()隐藏了using声明引入的B1 :: fb().

至于为什么两者都使用B1 :: d是不正确的;和int d;在B2的定义中,C标准(C03§7.3.3/ 10)解释了:

Since a using-declaration is a declaration,the restrictions on declarations of the same name in the same declarative region also apply to using-declarations.

因此,由于以下形式不正确,它的结构不正确:它会在单个声明区域中生成两个具有相同名称的对象:

struct S { int d; int d; };

(编辑:李大同)

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

    推荐文章
      热点阅读