GCC对char指针的处理和其他C++编译器处理不一样

hqylfy hqylfy 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
以下是代码
头文件:char_test.h
#ifndef CHAR_TESTH
#define CHAR_TESTH

#include <string.h>
#include <stdio.h>

class Test
{
public:
Test(char *p);
~Test();
operator char*() const;

private:
char *m_pBuf;
int m_nBufLen;
};

inline bool operator==(const Test& s1, const Test& s2)
{
return (strcmp(s1 , s2)==0);
}

inline bool operator==(const Test& s1, char * s2)
{
return (strcmp(s1 , s2) == 0) ;
}
#endif
CPP文件:char_test.cpp

#include "char_test.h"

Test::Test(char *p)
{
long nLen = strlen(p);
if (nLen == 0)
{
return;
}

m_pBuf = new char[nLen + 1];
strcpy(m_pBuf , p);
m_nBufLen = nLen + 1;
}
Test::~Test()
{
if (m_pBuf)
{
delete []m_pBuf;
}
}
Test::operator char*() const
{
return m_pBuf;
}
main文件:
#include <cstdlib>
#include <iostream>
#include "char_test.h"

using namespace std;

int main(int argc, char *argv[])
{
Test Test1("test");
if (Test1 == "test")
{
printf("==\r\n");
}else
{
printf("!=\r\n");
}
system("PAUSE");
return EXIT_SUCCESS;
}
这段程序在BCB6.0、VC6.0、AIX的xlC均编译通过,但是用GCC编译提示:
ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
bool operator==(const Test&, char*)
operator==(const char*, const char*) <built-in>
提示"=="操作符存在二义性,不知道是不是GCC对于这类对二义性审查要求更严格?
...全文

相关推荐

评论 19
收藏 已收藏
分享
返回顶部
hqylfy
C++ 语言 65,214 成员 250,514 内容