GCC对char指针的处理和其他C++编译器处理不一样
以下是代码
头文件: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对于这类对二义性审查要求更严格?