万能的C++漏了点什么吗?

可以说使用C++的过程是一个充满了惊喜的过程,因为常常的,会听说新鲜实用的语言特性,比如,函数重载:

   1: void func(int i);
   2: void func(double d);
   3: void func(const char* s);

在调用func的时候,根据所传参数的类型,编译器会自动选择合适的函数。

但是,在这个例子中,C++却没有给我惊喜:

   1: void func(const char* c){}
   2: struct _class
   3: {
   4:     void func(int i)
   5:     {
   6:     }
   7:     void call()
   8:     {
   9:         ::func("hello"); // work
  10:         func("hello");   // expected call global void func(const char* c){},
  11:                          // but doesnt compile
  12:     }
  13: };
  14:  
  15: int main()
  16: {
  17:     _class o;
  18:     o.call();
  19: }

编译出错:

error C2664: '_class::func' : cannot convert parameter 1 from 'const char [6]' to 'int'

窃以为C++其实可以给我这个惊喜的,为什么他没有给?要知道C++连模板参数都能推导出来!

posted on 2009-09-17 19:21  Tactoth  阅读(285)  评论(4编辑  收藏  举报

导航