C++编程思想:指针,引用,拷贝构造函数,赋值运算符
生活随笔
收集整理的這篇文章主要介紹了
C++编程思想:指针,引用,拷贝构造函数,赋值运算符
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 對指針的引用和指針的指針到底是怎么一回事?
- 函數返回引用到底返回了什么?
- 拷貝構造函數和默認拷貝構造函數
- 自定義拷貝構造函數
- 使用默認拷貝構造函數
- =賦值運算符重載
對指針的引用和指針的指針到底是怎么一回事?
#include<iostream>int i = 47; int* p = &i; void f(int** ip) {std::cout << "ip :" << ip <<std::endl;std::cout << "*ip :" << *ip <<std::endl;std::cout << "**ip :" << **ip <<std::endl; }void g(int* &dp) {std::cout << "dp :" << dp << std::endl;std::cout << "*dp :" << *dp << std::endl;//std::cout << "**dp :" << **dp << std::endl; //error } // Error //void h(int&* p) {} int main() {std::cout << "&i的值:" << &i << std::endl;std::cout << "i的值:" << i << std::endl;std::cout << "&p的值" << &p << std::endl;std::cout << "p的值" << p << std::endl;std::cout << "*p的值" << *p << std::endl;f(&p);g(p);system("pause");return 0; }
指針的引用:兩個指針指向同一個變量,解引用的內容相同,不能進行解引用的解引用
指針的指針:第一個指針指向另一個指針地址(第一個指針中存了第二個指針的地址),第一次解引用活得一個指針中的內容(指向一個對象的地址),第二次解引用獲得指針內容(地址)中的內容。
函數返回引用到底返回了什么?
函數返回引用,返回了一塊不會隨著函數結束而結束的內存,所以針對這個返回值,可以把它當作普通對象進行賦值,也可以對其進行引用。
int& f(int& x) {std::cout << "&x : " << &x << endl;return x; }int main() {int b = 1;std::cout << "&b " << &b<<endl;//int c = 1;//std::cout << "&c" << c;int& c = b;std::cout << "&c " << &c<<endl;int& d = f(b);int e = 1;std::cout << "&e " << &e << endl;e = f(b); //這里是一次賦值運算std::cout << "&f(b) " << &f(b) << endl;std::cout << "&d " << &d<<endl;std::cout << "&e " << &e << endl;}
從程序運行的結果看,對于f()函數的返回值有兩種使用方式,第一種只取其值,這樣的話變量有自己的地址。第二種不僅取其值,還取其地址。
拷貝構造函數和默認拷貝構造函數
拷貝構造函數的調用出席在用一個類初始化另一個類的時候。默認拷貝構造函數再將類內的指針傳遞時采取按值傳遞的方式這樣就會產生多個對象內的指針指向同一個地方。
自定義拷貝構造函數
#include <iostream> #include <string> using namespace std;static int countA = 0;class A { public:int* AI;public:A() :AI(nullptr){AI = new int(countA);countA++;std::cout << "this is A() : " << countA << endl;}A(const A &a){AI = new int(*a.AI);countA++;std::cout << "this is A(A&) : " << countA << endl;}~A() {countA--;std::cout << "this is ~A() : " << countA << endl;if (AI != nullptr);{std::cout << "*AI : " << *AI << endl;std::cout << "AI : " << AI << endl;}}A& operator = (const A& a){AI = a.AI;std::cout << "this is operator = : " << countA << endl;return *this;}};void fA(A a) {};int main() {A a1;A a2 = A(a1);fA(a1); }
指針的地址不一樣
使用默認拷貝構造函數
#include <iostream> #include <string> using namespace std;static int countA = 0;class A { public:int* AI;public:A() :AI(nullptr){AI = new int(countA);countA++;std::cout << "this is A() : " << countA << endl;}/*A(const A &a){AI = new int(*a.AI);countA++;std::cout << "this is A(A&) : " << countA << endl;}*/~A() {countA--;std::cout << "this is ~A() : " << countA << endl;if (AI != nullptr);{std::cout << "*AI : " << *AI << endl;std::cout << "AI : " << AI << endl;}}A& operator = (const A& a){AI = a.AI;std::cout << "this is operator = : " << countA << endl;return *this;}};void fA(A a) {};int main() {A a1;A a2 = A(a1);fA(a1); }
指針地址一樣。
=賦值運算符重載
#include <iostream> #include <string> using namespace std;static int countA = 0;class A { public:int* AI;public:A() :AI(nullptr){AI = new int(countA);countA++;std::cout << "this is A() : " << countA << endl;}A(const A &a){AI = new int(*a.AI);countA++;std::cout << "this is A(A&) : " << countA << endl;}~A() {countA--;std::cout << "this is ~A() : " << countA << endl;if (AI != nullptr);{std::cout << "*AI : " << *AI << endl;std::cout << "AI : " << AI << endl;delete AI;}}A& operator = (const A& a){if (this == &a) //避免自賦值{return *this;}AI = new int (*a.AI);std::cout << "this is operator = : " << countA << endl;return *this;}};void fA(A a) {};int main() {A a1;A a2 = A(a1);fA(a1);A a3;a3 = a1; }當對象中有指針時,必須自定義拷貝構造函數 和 賦值運算符 否則在析構函數刪除對象時會出錯。為了防止出錯可以刪完在賦值為nullptr(這并不能保證不會出錯).
#include <iostream> #include <string> using namespace std;static int countA = 0;class A { public:int* AI;public:A() :AI(nullptr){AI = new int(countA);countA++;std::cout << "this is A() : " << countA << endl;}//沒有拷貝構造函數會出錯A(const A &a){AI = new int(*a.AI);countA++;std::cout << "this is A(A&) : " << countA << endl;}~A() {countA--;std::cout << "this is ~A() : " << countA << endl;if (AI != nullptr);{std::cout << "*AI : " << *AI << endl;std::cout << "AI : " << AI << endl;delete AI;AI = nullptr;}}A& operator = (const A& a){if (this == &a) //避免自賦值{return *this;}AI = new int (*a.AI); //這樣不會產生錯誤//AI = a.AI; //這回產生錯誤std::cout << "this is operator = : " << countA << endl;return *this;}};void fA(A a) {};int main() {A a1;A a2 = A(a1);fA(a1);A a3;a3 = a1; }這個錯誤就是對于多個對象內的指針變量指向同一個地方,在析構函數中釋放內存會產生的錯誤
總結
以上是生活随笔為你收集整理的C++编程思想:指针,引用,拷贝构造函数,赋值运算符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++编译单元 内部链接 外部链接
- 下一篇: C++编程思想:父类函数隐藏