两个声明在C++中如何对应而不具有相同的名称?

huangapple go评论60阅读模式
英文:

How can two declarations correspond without having the same name in C++?

问题

我猜这是一个简单的问题,至少我不能想出任何例子。标准

> 如果它们(重新)引入相同的名称,都声明构造函数,或都声明析构函数,则两个声明对应,除非...

所以,如果我理解正确的话,如果我有两个类 A 和 B,两个构造函数 A() 和 B(),它们对应吗?也许我没有考虑到 unless。但在哪种情况下,声明会对应而不具有相同的名称呢?

英文:

I guess it's a simple question, at least I can't think in any example. standard:

> Two declarations correspond if they (re)introduce the same name, both
> declare constructors, or both declare destructors, unless...

So. If I understood correctly, if I have two classes A and B, two constructors A() and B(), they correspond? maybe I'm not considering the unless. But what would be such a example in which declarations correspond without having the same name?

答案1

得分: 2

"unless"之后的内容很重要。特别是这部分:

> * 每个声明一个函数或函数模板,除非:
> * 两者声明具有相同的非对象参数类型列表,等效的 ([temp.over.link]) 尾随 requires-clauses(如果有的话,除非在 [temp.friend] 中另有规定),如果两者都是非静态成员,则它们具有相应的对象参数,或者...

即使"corresponding object parameters"也有一个 definition,略高于上述段落。它包括文本,"它们的对象参数具有相同的类型。"

构造函数和析构函数不能有显式的对象参数类型。因此,它们的对象参数类型是 implicit 的,并且是从它们所属类的类型形成的 have "object parameter" types。因此,不同类的构造函数/析构函数具有不同的对象参数类型,因此它们的对象参数类型不"correspond",因此,这是"each declares a function"的一个例外情况,这意味着这些声明不"correspond"。

英文:

The stuff after "unless" is rather important. In particular, this:

> * each declares a function or function template, except when:
> * both declare functions with the same non-object-parameter-type-list,18 equivalent ([temp.over.link]) trailing requires-clauses (if any, except as specified in [temp.friend]), and, if both are non-static members, they have corresponding object parameters, or...

Even "corresponding object parameters" has a definition which is slightly above the above paragraph. It includes the text, "their object parameters have the same type."

Constructors and destructors cannot ahve explicit object parameter types. Therefore, their object parameter types are implicit and have "object parameter" types formed from the type of the class they're a member of. So constructors/destructors of different classes have different object parameter types, so their object parameter types do not "correspond", and therefore, this is an exception to the "each declares a function", which means that they these declarations do not "correspond".

huangapple
  • 本文由 发表于 2023年6月6日 01:05:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定