我正在寻找 Stadard 中描述以下行为的引述:
以下规则用于 conversion-type-id
查找 (3.4.6/7):
If the id-expression is a conversion-function-id, its conversion-type-id is first looked up in the class of the object expression and the name, if found, is used. Otherwise it is looked up in the context of the entire postfix-expression.
考虑以下示例:
#include <iostream>
class J{ public: static const char a = 'j'; };
typedef J Y;
class C
{
public:
operator Y(){ std::cout << Y::a; }
};
int main()
{
typedef Y Z;
C *c= new C();
c -> operator Z(); //C::operator Y is invoked
}
我不明白。上面的引述并没有描述这种行为。它仅描述了对 conversion-type-id
的查找,但未描述 conversion-function-id
本身。
conversion-function-id
的查找规则是什么?
最佳答案
如果显式使用conversion-function-id,如示例表达式c->operator Z()
,查找规则与任何其他类(class)成员。一个技巧是第 3 节第 8 段的定义:
Two names are the same if
they are identifiers composed of the same character sequence, or
they are operator-function-ids formed with the same operator, or
they are conversion-function-ids formed with the same type, or
they are template-ids that refer to the same class or function, or
they are the names of literal operators formed with the same literal suffix identifier.
此处的第三个选项是表达式中使用的名称 operator Z
找到类成员 C::operator Y()
的原因。它是访问类中的一个成员,与表达式的 conversion-function-id 同名。
关于c++ - 类成员转换函数-id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24107629/