本文介绍了为什么在C ++中不是(“ Maya” ==“ Maya”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么由于此代码而得到玛雅不是玛雅人的意思吗?

Any idea why I get "Maya is not Maya" as a result of this code?

if ("Maya" == "Maya") 
   printf("Maya is Maya \n");
else
   printf("Maya is not Maya \n");


推荐答案

因为您实际上是在比较两个指针-使用例如而是以下之一:

Because you are actually comparing two pointers - use e.g. one of the following instead:

if (std::string("Maya") == "Maya") { /* ... */ } 
if (std::strcmp("Maya", "Maya") == 0) { /* ... */ }

这是因为C ++ 03,§2.13.4表示:

This is because C++03, §2.13.4 says:

...,在您的情况下,需要进行指针转换。

... and in your case a conversion to pointer applies.

另请参见有关您为何无法为 == 在这种情况下。

See also this question on why you can't provide an overload for == for this case.

这篇关于为什么在C ++中不是(“ Maya” ==“ Maya”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 21:53