微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 使用声明(派生类)

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; };

原文地址:https://www.jb51.cc/c/110875.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐