| 网站首页 | JAVA文章 | AppServers | Web开发 | 应用开发 | 资源下载 | 论坛
    想学好编程,学好外语很重要  [enadd  2006年12月25日]        
设为首页 加入收藏 联系站长
您现在的位置: 编程笔记网 >> 应用开发 >> vc >> 平台系统 >> 文章正文
转:从VC++到GCC移植:谈两者语法差异            【字体:
转:从VC++到GCC移植:谈两者语法差异
作者:许式伟    文章来源:csdn    点击数:    更新时间:2007-3-2

作者:许式伟 (版权声明)

  类型引用

以下是引用片段:
  template
  class Foo
  {
  typedef T::SomeType SomeType;
  };

  这段代码在VC++中一点问题也没有,但是GCC并不允许,因为它不知道T::SomeType是什么。你需要改为:

以下是引用片段:
  template
  class Foo
  {
  typedef typename T::SomeType SomeType;
  };

  通过typename T::SomeType告诉GCC,SomeType是一个类型名,而不是其他东西。

  当然,这种情况不只是出现在typedef中。例如:

以下是引用片段:
  template
  void visit(const Container& cont)
  {
  for (Container::const_iterator it = cont.begin(); it != cont.end(); ++it)
  ...
  }

  这里的Container::const_iterator同样需要改为typename Container::const_iterator。
基类成员引用

以下是引用片段:
  template
  class Foo : public Base
  {
  public:
  void foo() {
  base_func();
  m_base_member = 0;
  }
  };

  这段代码在VC++中同样没有问题,但是GCC中不能通过。因为GCC并不知道base_func,m_base_member是什么。对于这个问题,你可以有两种改法:

  改法1:加上域作用符Base::

以下是引用片段:
  template
  class Foo : public Base
  {
  public:
  void foo() {
  Base::base_func();
  Base::m_base_member = 0;
  }
  };

  改法2:使用using指示符

以下是引用片段:
  template
  class Foo : public Base
  {
  public:
  using Base::base_func;
  using Base::m_base_member;
  void foo() {
  base_func();
  m_base_member = 0;
  }
  };

  这两种方法各有好处,在class Foo中出现大量的Base::base_func、m_base_member的引用时,使用using是方便的。而如果只有一次,那么方法1显得简短。
交叉引用许可

以下是引用片段:
  class SomeClass;
  template
  class Foo
  {
  public:
  void foo(SomeClass& a) {
  a.some_func();
  }
  void foo2() {
  SomeClass a;
  a.some_func();
  }
  };
  class SomeClass
  {
  public:
  void some_func() {
  ...
  }
  };

  由于VC++对模板函数的迟编译,因此,一个模板类不只是可以调用一个尚未出现的类成员函数(或者访问其成员变量),甚至可以定义其实例。这种语法对C++来说确实显得怪异。因为等到编译后面的SomeClass时,他又可以在其函数中定义class Foo的实例,从而出现交叉引用的情况。这在非模板函数的情形下就算你用VC++亦难以做到。

  遇到这种情况,该如何移植到GCC中?这个问题有点棘手。我个人认为出现这种情况是不太应该的,这意味着对类与类之间的关系混淆不清。你需要仔细审视一下这两个类正确的关系是什么。如果是移植库(例如WTL就有多处这样的情形)的过程中遇到这种情况,可以把函数的实现体改为放到类定义体外,如下:

以下是引用片段:
  class SomeClass;
  template
  class Foo
  {
  public:
  void foo(SomeClass& a);
  void foo2();
  };
  class SomeClass
  {
  public:
  void some_func() {
  ...
  }
  };
  template
  inline void Foo::foo(SomeClass& a) {
  a.some_func();
  }
  template
  inline void Foo::foo2() {
  SomeClass a;
  a.some_func();
  }

文章录入:fengyun    责任编辑:fengyun 
  • 上一篇文章:

  • 下一篇文章: 没有了
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
  • Visual C++编译器常用选项设…

  • 在VisualStudio中使用Window…

  • VC++开发的应用技巧三则放送

  • VC调试入门

  • Visual C++编程窃取QQ密码

  • VC时间控制函数

  • WTL for MFC Programming实践…

  • WTL for MFC Programming实践…

  • ATL ActiveX控件中用WTL::CB…

  • VC常用知识

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 管理登录 |