标签:
1 String::String(const String& str) 2 { 3 m_data=new char[strlen(str.m_data)+1]; 4 strcopy(m_data,str.m_data); 5 }
1 String& String::operator = (const String& str) 2 { 3 if(this == &str)//为了防止释放自身指针 4 { 5 return *this; 6 } 7 delete[] m_data; 8 m_data = new char[strlen(str.data) + 1]; 9 strcpy(m_data, str.data); 10 return *this; 11 }
(PS:内存泄露:手动分配的内存在进程结束前是不会被自动释放的,如不再使用需要手动释放其内存;)
1 template <class T, class Squence = deque<T> > 2 class queue 3 { 4 //... 5 protected: 6 Squence c; //底层容器 7 public: 8 //完全依赖于c的功能函数 9 bool empty() const { return c.empty(); } 10 size_type size() const { return c.size(); } 11 reference front() { return c.front(); } 12 reference back() { return c.back(); } 13 //deque 是两头都能出,queue是先进先出(只有一个出口) 14 void push(const value_type& x) { c.push_back(x); } 15 void pop() { c.pop_front(); } 16 };
这是一个列队模板函数,若类函数在类外定义时需要在定义前加上模板参数
1 template <class T, class Squence = deque<T> > void push(const value_type& x) {c.push_back(x);}
标签:
原文地址:http://www.cnblogs.com/DecepH/p/notesforgeekband_2.html