c – 使用声明(派生类)

前端之家收集整理的这篇文章主要介绍了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.f2er.com/c/110875.html

猜你在找的C&C++相关文章