operator const char *() [英] operator const char*()

查看:92
本文介绍了operator const char *()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct C

{

operator const char *(){return" ...." ;; }

};

int main()

{

C a;

0 [a];

}


为什么那个0 [a]调用操作符const char *()?

解决方案

vineoff写道:

struct C
{
operator const char *(){return"。 ...英寸; }
};
int main()
{
C a;
0 [a];
}

为什么那个0 [a]调用操作符const char *()?




不确定,但这里是猜测。


以e1 [e2]为例。这与*(e1 + e2)完全相同。由于

加法是可交换的,这与*(e2 + e1)相同,这也是相当于e2 [e1]的
。我想因为编译器不能下标一个

整数,它会反转下标,使其成为


a [0];


并将''a''转换为const char *。

Jonathan


在c语言中,[ 0]和0 [a]是等价的。


是的,[0]也调用该运算符,但为什么呢?

struct C
{
operator const char*() { return "...."; }
};
int main()
{
C a;
0[a];
}

Why does that 0[a] call operator const char*() ?

解决方案

vineoff wrote:

struct C
{
operator const char*() { return "...."; }
};
int main()
{
C a;
0[a];
}

Why does that 0[a] call operator const char*() ?



Not sure, but here''s a guess.

Take for example e1[e2]. This is exactly like *(e1 + e2). Since
addition is commutative, this is the same as *(e2 + e1), which is again
equivalent to e2[e1]. I suppose since the compiler cannot subscript an
integer, it reverses the subscript, making it

a[0];

and converts ''a'' to a const char*.
Jonathan


In c language, a[0] and 0[a] is equivalent.


Yes, a[0] calls that operator too, but why?


这篇关于operator const char *()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆