Re: [cpp] 关于const char * const &

ling xl-yc-lzj at necsoft.com.cn
Wed Jul 19 16:12:40 CST 2006


不好意思,我知道原因了,应该是头文件iostream中有一个同名的函数min.

----- Original Message ----- 
  From: ling
  To: C++ Discuss Group
  Sent: Wednesday, July 19, 2006 5:04 PM
  Subject: [cpp] 关于const char * const &



  请问下面代码的函数min和min1有什么区别啊。
  为什么函数min取得的比较结果不对呢?另外如果数组a,b的声明
  前面加上const的话,函数min取得的结果就对了。
  函数min1不管有没有const都能得到正确的结果。

  #include <cstring>
  #include <iostream>
  using namespace std;

  const char* const& min(const char* const& a, const char* const& b)
  {
   cout << "a = " << a << endl;
   cout << "b = " << b << endl;
   return (strcmp(a, b) < 0) ? a : b;
  }

  const char* const min1(const char* const a, const char* const b)
  {
   return (strcmp(a, b) < 0) ? a : b;
  }

  int main()
  {
   char a[] = "123";
   char b[] = "456";

   cout << min(a, b) << endl;
    cout << min1(a, b) << endl;
  }


------------------------------------------------------------------------------


  _______________________________________________
  Cpp mailing list
  Cpp at codingnow.com
  http://codingnow.com/mailman/listinfo/cpp
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://codingnow.com/pipermail/cpp/attachments/20060719/bb47ebf3/attachment.html


More information about the Cpp mailing list