鱼C论坛

 找回密码
 立即注册
查看: 3181|回复: 2

[技术交流] 各种构造函数,可以学习。

[复制链接]
发表于 2014-2-22 16:32:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
具体的我就不解释了,运行一下大家就会明白这个小程序的教学意义了。~帮助理解构造函数、按值传递
  1. #include <iostream>
  2. #include <cstring>

  3. class StringVal
  4. {
  5.         friend StringVal operator+ (StringVal const &v1, StringVal const &v2);
  6. public:
  7.         StringVal (char const * cstr)
  8.                 :_buf (0)
  9.         {
  10.                 std::cout << "Constructor taking char * "
  11.                                   << cstr << std::endl;
  12.                 Init (cstr);
  13.         }
  14.         StringVal (StringVal const & str)
  15.                 :_buf (0)
  16.         {
  17.                 std::cout << "Copy constructor "
  18.                                   << str.c_str () << std::endl;
  19.                 Init (str.c_str ());
  20.         }
  21.         ~StringVal ()
  22.         {
  23.                 std::cout << "Destructor of " << _buf << std::endl;
  24.                 delete _buf;
  25.         }
  26.         StringVal & operator= (StringVal const & str);
  27.         char const * c_str () const { return _buf; }
  28. private:
  29.         StringVal () : _buf (0) {}
  30.         void Init (char const * cstr)
  31.         {
  32.                 _buf = new char [std::strlen (cstr) + 1];
  33.                 std::strcpy (_buf, cstr);
  34.         }
  35. private:
  36.         char  * _buf;
  37. };

  38. StringVal & StringVal::operator= (StringVal const & str)
  39. {
  40.         std::cout << "Operator = " << str.c_str () << std::endl;
  41.         if (this != &str)
  42.         {
  43.                 if (std::strlen (_buf) < std::strlen (str.c_str ()))
  44.                 {
  45.                         delete _buf;
  46.                         Init (str.c_str ());
  47.                 }
  48.                 else
  49.                         std::strcpy (_buf, str.c_str ());
  50.         }
  51.         return *this;
  52. }

  53. inline StringVal operator+ (StringVal const &v1, StringVal const &v2)
  54. {
  55.         std::cout << "  operator + (" << v1.c_str () << ", "
  56.                 << v2.c_str () << ")\n";
  57.                
  58.         StringVal result; // empty
  59.         int len = std::strlen (v1._buf) + std::strlen (v2._buf);
  60.         char * buf = new char [len + 1];
  61.         std::strcpy (buf, v1.c_str ());
  62.         std::strcat (buf, v2.c_str ());
  63.         result._buf = buf;
  64.         std::cout << "  Returning by value\n";
  65.         return result;
  66. }

  67. StringVal ByValue ();

  68. StringVal ByValue ()
  69. {
  70.         StringVal str ("Bar");
  71.         return str;
  72. }

  73. int main ()
  74. {
  75.         StringVal str ("Foo");
  76.         str = ByValue ();
  77.         StringVal concat = str + "tosz";
  78.         return 0;
  79. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-2-22 16:53:53 | 显示全部楼层
C++比C烦 ,幕后工作太多
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-23 07:10:03 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 17:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表