|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
具体的我就不解释了,运行一下大家就会明白这个小程序的教学意义了。~帮助理解构造函数、按值传递
- #include <iostream>
- #include <cstring>
- class StringVal
- {
- friend StringVal operator+ (StringVal const &v1, StringVal const &v2);
- public:
- StringVal (char const * cstr)
- :_buf (0)
- {
- std::cout << "Constructor taking char * "
- << cstr << std::endl;
- Init (cstr);
- }
- StringVal (StringVal const & str)
- :_buf (0)
- {
- std::cout << "Copy constructor "
- << str.c_str () << std::endl;
- Init (str.c_str ());
- }
- ~StringVal ()
- {
- std::cout << "Destructor of " << _buf << std::endl;
- delete _buf;
- }
- StringVal & operator= (StringVal const & str);
- char const * c_str () const { return _buf; }
- private:
- StringVal () : _buf (0) {}
- void Init (char const * cstr)
- {
- _buf = new char [std::strlen (cstr) + 1];
- std::strcpy (_buf, cstr);
- }
- private:
- char * _buf;
- };
- StringVal & StringVal::operator= (StringVal const & str)
- {
- std::cout << "Operator = " << str.c_str () << std::endl;
- if (this != &str)
- {
- if (std::strlen (_buf) < std::strlen (str.c_str ()))
- {
- delete _buf;
- Init (str.c_str ());
- }
- else
- std::strcpy (_buf, str.c_str ());
- }
- return *this;
- }
- inline StringVal operator+ (StringVal const &v1, StringVal const &v2)
- {
- std::cout << " operator + (" << v1.c_str () << ", "
- << v2.c_str () << ")\n";
-
- StringVal result; // empty
- int len = std::strlen (v1._buf) + std::strlen (v2._buf);
- char * buf = new char [len + 1];
- std::strcpy (buf, v1.c_str ());
- std::strcat (buf, v2.c_str ());
- result._buf = buf;
- std::cout << " Returning by value\n";
- return result;
- }
- StringVal ByValue ();
- StringVal ByValue ()
- {
- StringVal str ("Bar");
- return str;
- }
- int main ()
- {
- StringVal str ("Foo");
- str = ByValue ();
- StringVal concat = str + "tosz";
- return 0;
- }
复制代码
|
|