您的位置:程序门 -> c/c++ -> c++ 语言



为什么在两个编译器上结果不一样啊。请高手指教


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


为什么在两个编译器上结果不一样啊。请高手指教[已结贴,结贴人:liqinghua1653]
发表于:2008-01-21 09:44:32 楼主
c/c++ code
//---test.cpp---- //compile with: cl test.cpp struct b { void operator[](int index) const{} }; struct c { void operator[](int index) const{ } }; struct a:public b, public c { }; void main() { a a; a[1]; }

在vc2005中是下面结果,没有编译错误。
microsoft   (r)   32-bit   c/c++   optimizing   compiler   version   14.00.50727.762   for   80x86

copyright   (c)   microsoft   corporation.     all   rights   reserved.

test.cpp
microsoft   (r)   incremental   linker   version   8.00.50727.762
copyright   (c)   microsoft   corporation.     all   rights   reserved.

/out:test.exe
test.obj

但是在哦orcas上面是:
microsoft   (r)   32-bit   c/c++   optimizing   compiler   version   15.00.20706.05   for   80x86
copyright   (c)   microsoft   corporation.     all   rights   reserved.

test.cpp
test.cpp(19)   :   error   c2593:   'operator   ['   is   ambiguous
                test.cpp(3):   could   be   'void   b::operator   [](int)   const'
                test.cpp(8):   or               'void   c::operator   [](int)   const'
                while   trying   to   match   the   argument   list   '(a,   int)'

为什么不一样啊,是某一个编译器的bug吗,还是编译器有改变或改进。

发表于:2008-01-21 09:46:451楼 得分:0
我记得标准里面是支持orcas的,看看c++primer
发表于:2008-01-21 10:00:282楼 得分:5
装个gcc再试。另外,自己试一下到底vc2005调用了哪个[]。
发表于:2008-01-21 11:27:363楼 得分:0
gcc上和orcas结果一样,
在utc(vs2005)上调用了struct   b   中的[](继承时哪个前面调用哪个)
发表于:2008-01-21 11:48:484楼 得分:5
那么,你应该知道结论了:不要写挑战编译器的代码。
发表于:2008-01-21 11:51:565楼 得分:5
primer说过(大概是这样)即使多继承的不同父类里几个同名函数可以构成重载,在调用的时候也需要显式指定
何况你的情况是两个同名函数的签名也类似
发表于:2008-01-21 12:15:336楼 得分:5
在c++标准中,   别说你这两个函数参数完全相同,   就算是不一样的参数,   都会出现编译错误.

在多重继承中,   如果两个父类拥有同样名字的函数,   将会出现二义性,   无论参数是否相同,   均不会发生重载.   调用他们的唯一办法是显示的指明他们的域(父类名).

举个例子:

c/c++ code
#include <iostream> using namespace std; class f1 { public: void foo(int i) {cout<<"f1 ="<<i<<endl;} }; class f2 { public: void foo(int i, int j) { cout<<"f2="<<i<<' '<<j<<endl;} }; class s1 : public f1, public f2 { }; int main() { s1 s; s.f1::foo(10); //ok s.f2::foo(10, 20); //ok s.foo(30); //错误, 二义性的调用 s.foo(30, 40); //错误, 二义性的调用 return 0; }

vs2005能够运行,   说明他不符合这条c++标准罢了.


快速检索

最新资讯
热门点击