编译器的差异:临时对象做实参的可能编译不过?
下面的代码在vs2005可正常编译运行,但gcc似乎不行(偶用的cygwin),虽然vs2005对标准C++的支持已经有很大进步了。。
gcc版本如下:
D:\cygwin\bin>gcc -v
Reading specs from /usr/lib/gcc/i686-pc-cygwin/3.4.4/specs
Configured with: /gcc/gcc-3.4.4/gcc-3.4.4-1/configure --verbose --prefix=/usr --exec-prefix=/usr --sysconfdir=/etc --lib
dir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --enable-languages=c,ada,c++,d,f77,
java,objc --enable-nls --without-included-gettext --enable-version-specific-runtime-libs --without-x --enable-libgcj --d
isable-java-awt --with-system-zlib --enable-interpreter --disable-libgcj-debug --enable-threads=posix --enable-java-gc=b
oehm --disable-win32-registry --enable-sjlj-exceptions --enable-hash-synchronization --enable-libstdcxx-debug : (reconfi
gured)
Thread model: posix
gcc version 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125)
言归正传,
问题一:
void fun2(string &str)
{
cout << str << endl;
}
int main()
{
fun2(string("hello, "));
}
gcc编译错误提示如下:
D:\cygwin\home\ADMINI~1\project\aboutCPP.cpp: In function `int main()':
D:\cygwin\home\ADMINI~1\project\aboutCPP.cpp:35: error: invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'std::string'
D:\cygwin\home\ADMINI~1\project\aboutCPP.cpp:28: error: in passing argument 1 of `void fun2(std::string&)'
为什么非法?看起来似乎是由于临时对象是一个const对象?真的吗?
答案是否定的,下面的代码可以说明之:
class MyString
{
public:
int Data() { return m_data; }
MyString &Data(int val) { m_data = val; }
private:
int m_data;
};
int main()
{
cout << MyString().Data(2).Data() << endl;
}
说明临时对象是non-const的!
另外,无意中又发现一个问题。
问题二:
class MyString
{
public:
int Data() { return m_data; }
MyString &Data(int val) { m_data = val; }
private:
int m_data;
};
int main()
{
MyString conMyStr;
const MyString conMyStr2;
}
gcc编译报错如下:
D:\cygwin\home\ADMINI~1\project\aboutCPP.cpp: In function `int main()':
D:\cygwin\home\ADMINI~1\project\aboutCPP.cpp:51: error: uninitialized const `conMyStr2'
gcc对const的支持有问题吗(瞎猜的,怎可能)?这个错又怎么解释?
小弟比较穷,分不多,但暂时也找不到好的解释办法。
哪位大侠可指点一下,谢谢~!