使用声明(派生类)
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-
对于非转换函数的引入 通过使用声明到派生 类,该函数被认为是 是派生类的成员 定义类型的目的 隐式对象参数。
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(){}
Is it because, B1::fb()
is treated as B1::fb(B1*)
and B2::fb()
treated as B2::fb(B2*)
? That is, does the implicit parameter, help in distinguishing these?
$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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C++ 标准 (C++03 §7.3.3/12) 解释道:
在您的示例中,
B2::fb()
隐藏了 using 声明引入的B1::fb()
。至于为什么在
B2
的定义中同时使用using B1::d;
和int d;
是格式错误的,C++标准 (C++03 §7.3.3/10) 解释:因此,它是格式错误的出于同样的原因,以下内容格式不正确:它会在单个声明区域中产生两个具有相同名称的对象:
The C++ standard (C++03 §7.3.3/12) explains:
In your example,
B2::fb()
hides theB1::fb()
introduced by the using declaration.As for why it is ill-formed to have both
using B1::d;
andint d;
in the definition ofB2
, the C++ standard (C++03 §7.3.3/10) explains:So, it is ill-formed for the same reason that the following is ill-formed: it results in two objects with the same name in a single declarative region: