C++面向对象高级开发No.2 - Class With Pointer
2015-08-08 21:21
519 查看
Item:
先考虑测试用例, 从测试用例考虑函数实现
如果 class with pointer 那么 BigThree 都要自己重新写, 不能用编译器自带的.
拷贝构造函数 copy constructor
析构函数 Destructor
拷贝赋值函数(运算符) copy assignment operator
使用 new( ) 分配内存,则需要用 delete( )在相同的作用域内对应搭配,防止内存泄露;
Detail: Effective C++ Item16: Use the same form in corresponding uses of new and delete.
new( ): distribute memory -> call ctor
delete( ): call dtor -> free memory
array new 需要搭配 array delete: 否则引起内存泄露: 只调用了 p 的 dtor, 无法调用 p+1, p+2 的 dtor, 而母体块已经 free 掉.
Code:
operator= : 检测自我赋值 -> delete 原有 -> new 足够空间 -> copy
检测自我赋值: 防止delete后访问,导致未定义行为!
Detail: Effective C++ Item11: Handle assignment to self in operator=
令 operator= 返回一个 reference to *this : 实现“连锁赋值”
Detail: Effective C++ Item10: Have assignment operators return a reference to *this
Task:
Realize string, a class without pointer member, including merbers as following:
private member 1: char* m_data
public member 1: constructer: String(const char* ctr = 0);
public member 2: Big Three:
String(const String&);
~String( );
String& operator = (const String&) ;
public member 3: support function: char* get_c_str() to support output operator
Some global functions to use String: out put operator >>;
Code:
先考虑测试用例, 从测试用例考虑函数实现
如果 class with pointer 那么 BigThree 都要自己重新写, 不能用编译器自带的.
拷贝构造函数 copy constructor
析构函数 Destructor
拷贝赋值函数(运算符) copy assignment operator
使用 new( ) 分配内存,则需要用 delete( )在相同的作用域内对应搭配,防止内存泄露;
Detail: Effective C++ Item16: Use the same form in corresponding uses of new and delete.
new( ): distribute memory -> call ctor
delete( ): call dtor -> free memory
array new 需要搭配 array delete: 否则引起内存泄露: 只调用了 p 的 dtor, 无法调用 p+1, p+2 的 dtor, 而母体块已经 free 掉.
Code:
[code]string *p = new string[3]; ... delete p[];
operator= : 检测自我赋值 -> delete 原有 -> new 足够空间 -> copy
检测自我赋值: 防止delete后访问,导致未定义行为!
Detail: Effective C++ Item11: Handle assignment to self in operator=
令 operator= 返回一个 reference to *this : 实现“连锁赋值”
Detail: Effective C++ Item10: Have assignment operators return a reference to *this
Task:
Realize string, a class without pointer member, including merbers as following:
private member 1: char* m_data
public member 1: constructer: String(const char* ctr = 0);
public member 2: Big Three:
String(const String&);
~String( );
String& operator = (const String&) ;
public member 3: support function: char* get_c_str() to support output operator
Some global functions to use String: out put operator >>;
Code:
[code]// myString_test.cpp // Item1: test case -> function realization #include <iostream> #include "myString.h" int main() { String str1; String str2("Hello"); std::cout << str1 << std::endl; std::cout << str2 << std::endl; str1 = str2; String str3(str2); std::cout << "str1:" << str1 << std::endl; std::cout << "str2:" << str2 << std::endl; std::cout << "str3:" << str3 << std::endl; return 0; }
[code]// myString.h #ifndef MY_STRING_H #define MY_STRING_H class String; class String { public: String(const char* cstr = 0); String(const String& str); String& operator = (const String& str); ~String(); char* get_c_str() const { return m_data; } private: char* m_data; }; #include <cstring> // Item2: Big Three : copy constructor / destructer / copy assignment operator inline String::String(const char* cstr) { if (cstr) { m_data = new char[strlen(cstr) + 1]; strcpy(m_data, cstr); } else { // no initialization value m_data = new char[1]; *m_data = '\0'; } } inline String::~String() { delete [] m_data; // Item3: array new <-> array delete } inline String::String(const String& str) { m_data = new char[strlen(str.m_data) + 1]; strcpy(m_data, str.m_data); } inline String& String::operator = (const String& str) // & after typename: reference { // self assignment if(this == &str) // & before object: address of return *this; delete[] m_data; m_data = new char[strlen(str.m_data) + 1]; strcpy(m_data, str.m_data); return *this; // *this / str is a String } inline std::ostream& operator << (std::ostream& os, const String& str) { os << str.get_c_str(); return os; } #endif
相关文章推荐
- C++面向对象高级开发No.1 - Class Without Pointer
- 为什么和其他语言相比C语言是快速的语言
- C语言:顺序结构(数组)的循环队列
- leetcode_Valid Anagram_easy
- C++中数字与字符串之间的转换
- 使用c/c++扩展python
- C++下实现形成文本的一种方法
- C++文件操作
- C语言堆栈入门——堆和栈的区别
- C++(33)模板与泛型之实例化
- C++(32)
- C++(30)构造函数和复制控制
- C++(29)基类和派生类
- C++(28)自增/自减操作符
- C++(27)输入/输出、算术/关系操作符
- C++(26)重载操作符的定义
- C++(25)管理指针成员
- C++(24)消息处理示例
- C语言文件读写操作
- C++对象模型——Member的各种调用方式(第四章)