C++网易云课堂开发工程师-拷贝构造,拷贝复制,析构函数
1.帶有指針的Class,Class with pointer member
當類內(nèi)帶指針,一定自己寫出拷貝構造函數(shù)。
String s1();
String s2("hello");
String s3(s1);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 拷貝構造
s3=s2;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 拷貝賦值
往往編譯器會自動完成拷貝構造與拷貝賦值,不帶有指針的可以一個數(shù)據(jù)成員一個數(shù)據(jù)成員的賦值。
但是帶有指針時,會出現(xiàn)不同的效果。
2.Class String
class String{
public:
String(const char* cstr = 0);
String(const String& str);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 接受自己本身,我們稱之為拷貝構造
String& operator = (const String& str);? ? ? ? ? ? ? 拷貝賦值
~String();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?析構函數(shù)
char* get_c_str() const {return m_data;}
private:
char* m_data;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
}
3.ctor和dtor(構造函數(shù)和析構函數(shù))
inline
String::String(const char* cstr = 0){
if(cstr){
m_data = new char[strlen(cstr) + 1];
strcpy(m_data, cstr);
}
else{? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?未定義初值
m_data = new char[1];
*m_data = '\0';
}
}
?
inline? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 做動態(tài)分配后,一定要釋放掉
String::~String(){
delete[] m_data;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 搭配中括號的delete
}
4.類的創(chuàng)建與調(diào)用
{
String s1();? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
String s2("hello");? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 離開作用域后,析構函數(shù)自然而然會被調(diào)用
String* p = new String("hello");
delete p;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?必須使用delete搭配刪除
}
~~~~~~~~~~~一共要調(diào)用三次析構函數(shù)~~~~~~~~~~~~~
5.class with pointer members 必須有copy ctor和 copy op=
String a("hello");? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 對應的是? ? ? ? ? ? ? ? ? ? ? ? ? ? ? a---------->Hello\0
String b("World");? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?對應的是? ? ? ? ? ? ? ? ? ? ? ? ? ? ? b---------->World\0
使用default copy ctor 或者 default op= 會造成以下局面
a--------------->Hello\0<------------------b? ? ? ? ? ?World\0? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 淺拷貝
~~~~~~~必須完成深拷貝,主要是通過創(chuàng)建數(shù)據(jù)副本~~~~~~~~~
inline
String::String(const String& str){
m_data = new char[strlen(str.m_data) + 1];? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?創(chuàng)建數(shù)據(jù)副本
strcpy(m_data, str.m_data);
}
~~~拷貝賦值,因為當前創(chuàng)建空間所以一定要清空自己,而后進行賦值~~~
~~~拷貝構造不需要完成清空自己是因為它剛剛創(chuàng)建出來的~~~~~~~~
inline
String& String::operator= (const String& str){
if(this == &str){? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 檢測自我賦值/如果這兩行沒有寫檢測,甚至會造成結(jié)果出錯。
return *this;
}
delete[] m_data;
m_data = new char[strlen(str.m_data) + 1];
strcpy(m_data,? str.m_data);
return *this;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
如果if語句沒有寫,會造成自己銷毀自己,而不會完成賦值操作。
轉(zhuǎn)載于:https://www.cnblogs.com/sky-z/p/9505750.html
總結(jié)
以上是生活随笔為你收集整理的C++网易云课堂开发工程师-拷贝构造,拷贝复制,析构函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python网络编程之黏包问题
- 下一篇: s3c2440移植MQTT