正文

《Thinking In C++》第2卷   更正2009-08-01 09:58:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/shao/45932.html

分享到:

在《Thinking In C++》(第2卷)这本书中,第5章里面的Lookup2这个程序:   #include <algorithm>#include <iostream>#include <typeinfo>using std::cout;using std::endl; void g() { cout << "global g()" << endl; } template<class T> class Y {public:  void g() {    cout << "Y<" << typeid(T).name() << ">::g()" << endl;  }  void h() {    cout << "Y<" << typeid(T).name() << ">::h()" << endl;  }  typedef int E;}; typedef double E; template<class T> void swap(T& t1, T& t2) {  cout << "global swap" << endl;  T temp = t1;  t1 = t2;  t2 = temp;} template<class T> class X : public Y<T> {public:  E f() {    g();    this->h();    T t1 = T(), t2 = T(1);    cout << t1 << endl;    swap(t1, t2);    std::swap(t1, t2);    cout << typeid(E).name() << endl;    return E(t2);  }}; int main() {  X<int> x;  cout << x.f() << endl;} ///:~   书本的输出是  global g() Y<int>::h() 0 global swap double 1   在vs2008中正确的输出应该是: Y<int>::g() Y<int>::h() 0 global swap int 1 至于原因,应该是从公有继承和关联参数查找这方面考虑。  

阅读(2420) | 评论(2)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册