拷貝構(gòu)造
拷貝構(gòu)造函數(shù)是構(gòu)造函數(shù)的一種扭屁,當(dāng)利用已經(jīng)存在的對象創(chuàng)建一個新對象時,類似于拷貝需曾,就會調(diào)用新對象的拷貝構(gòu)造函數(shù)進行初始化够傍,拷貝構(gòu)造函數(shù)的格式是固定的形娇,接受一個const引用作為參數(shù)男图。
#include <iostream>
using namespace std;
class Car {
int m_price;
public:
Car(int price = 0):m_price(price){
cout << "Car(int price = 0):m_price(price)" << endl;
}
Car(const Car &car):m_price(car.m_price){
cout << "Car(const Car &car):m_price(car.m_price)"
<< endl;
}
};
int main() {
Car car1; //調(diào)用構(gòu)造函數(shù)
Car car2 = car1; //調(diào)用拷貝構(gòu)造函數(shù)
return 0;
}
調(diào)用父類發(fā)拷貝構(gòu)造函數(shù),如下所示:
#include <iostream>
using namespace std;
class Person {
protected:
int m_age;
public:
Person(int age = 0) :m_age(age) {
cout << "Person constructed function" << endl;
}
Person(const Person &person) :m_age(person.m_age) {
cout << "Person constructed copy function" << endl;
}
};
class Student : public Person {
int m_score;
public:
Student(int age = 0, int score = 0) :Person(age), m_score(score) {
cout << "Student constructed function" << endl;
}
Student(const Student &student) :Person(student), m_score(student.m_score) {
cout << "Student constructed copy function" << endl;
cout << this->m_age << endl;
}
};
int main() {
Student s1(10,100); //先調(diào)用父類的構(gòu)造函數(shù)再調(diào)用自己的構(gòu)造函數(shù)
Student s2 = s1;//先調(diào)用父類的拷貝構(gòu)造函數(shù)再調(diào)用自己的拷貝構(gòu)造函數(shù)
s2 = s1; //賦值操作甜橱,默認淺復(fù)制,不會調(diào)用拷貝構(gòu)造函數(shù)
return 0;
}
一般來說對象之間的賦值是都是淺復(fù)制逊笆,淺拷貝,編譯器默認提供的都是淺復(fù)制(淺拷貝)岂傲,即將對象所有的值賦值到另一個對象难裆,這種清空下,如果對象類型有指針類型镊掖,淺拷貝只是拷貝指針存儲的地址乃戈,指針指向的內(nèi)容是不會拷貝的,這時就會出現(xiàn)多個指針指向同一個對象亩进,對象釋放的時候會多次釋放出現(xiàn)問題症虑,此時解決方案就是自定義拷貝構(gòu)造函數(shù),對指針指向的內(nèi)容也進行拷貝归薛,從而達到深拷貝的目的谍憔。下面舉一個深拷貝的例子說明:
#include <iostream>
using namespace std;
class Car {
int m_price;
char *m_name; //含有char *變量
public:
Car(int price = 0, const char *name = NULL) :m_price(price) {
if (name == NULL) return;
// 申請堆空間存儲字符串內(nèi)容
this->m_name = new char[strlen(name) + 1]{};
// 拷貝字符串內(nèi)容到堆空間(string copy)
strcpy(this->m_name, name);
cout << "Car(int, const char *)" << endl;
}
Car(const Car &car) :m_price(car.m_price) {
if (car.m_name == NULL) return;
// 申請堆空間存儲字符串內(nèi)容
this->m_name = new char[strlen(car.m_name) + 1]{};
// 拷貝字符串內(nèi)容到堆空間(string copy)
strcpy(this->m_name, car.m_name);
cout << "Car(const Car &car)" << endl;
}
~Car() {
if (this->m_name == NULL) return;
delete[] this->m_name;
this->m_name = NULL;
cout << "~Car()" << endl;
}
void display() {
cout << "price is " << this->m_price << ", name is " << this->m_name << endl;
}
};
int main() {
//構(gòu)造char *的時候,構(gòu)造函數(shù)需要拷貝字符串到內(nèi)容主籍,不然可能會出現(xiàn)堆空間成內(nèi)存指向椣捌叮空間內(nèi)存
char name[] = { 'b', 'm', 'w', '\0' };
Car *car = new Car(100, name);
car->display();
delete car;
Car car1(100, "bmw");
// 將car1的內(nèi)存空間(8個字節(jié))覆蓋car2的內(nèi)存空間(8個字節(jié)自定義拷貝構(gòu)造函數(shù)會進行深拷貝)
Car car2 = car1;
return 0;
}
對象作為參數(shù)和返回值的時候,有些情況會調(diào)用拷貝構(gòu)造函數(shù)千元,有些函數(shù)不會調(diào)用拷貝構(gòu)造函數(shù)苫昌,不同編譯器優(yōu)化可能不一樣。
#include <iostream>
using namespace std;
class Car {
public:
int m_price;
Car(int price = 0) :m_price(price) {
cout << "Car(int) - " << this << " - " << this->m_price << endl;
}
Car(const Car &car) :m_price(car.m_price) {
cout << "Car(const Car &) - " << this << " - " << this->m_price << endl;
}
};
void test1(Car car) {
}
Car test2() {
Car car(10);
return car;
}
int main() {
Car car1(10);
test1(car1); //當(dāng)對象作為函數(shù)參數(shù)的時候會調(diào)用拷貝構(gòu)造函數(shù)诅炉,用引用就不會
Car car2 = test2(); //這里返回一;個臨時對象屋厘,不同編譯器優(yōu)化可能不一樣涕烧,我這里沒有沒有調(diào)用拷貝構(gòu)造函數(shù)
Car car3;
car3 = test2();//這里返回一個臨時對象,不同編譯器優(yōu)化可能不一樣汗洒,我這里沒有沒有調(diào)用拷貝構(gòu)造函數(shù)
return 0;
}
匿名對象
沒有變量名议纯,沒有被指針指向的對象,用完后馬上調(diào)用析構(gòu)函數(shù)溢谤,參考如下:
#include <iostream>
using namespace std;
class Person {
public:
int m_age;
Person(int age):m_age(age) {
cout << "Person() - " << this << endl;
}
Person(const Person &person) {
cout << "Person(const Person &person) - " << this << endl;
}
~Person() {
cout << "~Person() - " << this << endl;
}
void display() {
cout << "display()" << endl;
}
};
void test1(Person person) {
}
Person test2() {
// Person person;
return Person(5); //匿名對象
}
int main() {
Person p = test2();
cout << p.m_age << endl;
Person(10); //匿名對象
return 0;
}
在某些特定情況下瞻凤,會隱式調(diào)用函數(shù)的單參數(shù)的構(gòu)造函數(shù),比如
Person p = 10; //隱式構(gòu)造
我們可以加上關(guān)鍵字explicit來禁止函數(shù)的隱式構(gòu)造世杀。
Person(int age):m_age(age) {
cout << "Person() - " << this << endl;
}
默認構(gòu)造函數(shù)
C++編譯器在某些特定情況下阀参,會給類自動生成無參的構(gòu)造函數(shù),比如瞻坝。
- 成員變量在聲明的同時進行了初始化蛛壳。
- 有定義虛函數(shù)
- 虛繼承了其它類
- 包含了對象類型的成員
- 父類有構(gòu)造函數(shù),編譯器生成或自定義
通過以上規(guī)律可以看出,對象創(chuàng)建后衙荐,需要做一些額外操作時(內(nèi)存操作捞挥,函數(shù)調(diào)用)編譯器一般都會為其自動生成無參的構(gòu)造函數(shù)。編譯器是否生成了無參數(shù)的構(gòu)造函數(shù)忧吟,通過斷點查看反匯編函數(shù)調(diào)用過程砌函。
友元
友元包括友元函數(shù)和友元類
- 如果將函數(shù)A(非成員函數(shù))聲明為類C的友元函數(shù),那么函數(shù)A就能直接訪問類C對象的所有成員溜族,如果將類A聲明為類C的友元類讹俊,那么類A的所有成員函數(shù)都能直接訪問類C對象的所有成員
-友元破壞了面向?qū)ο蟮奶匦裕谀承╊l繁訪問成員變量的地方可以提高性能斩祭。
#include <iostream>
using namespace std;
class Point {
friend Point add(const Point &, const Point &);
friend class Math;
private:
int m_x;
int m_y;
public:
int getX() const { return this->m_x; };
int getY() const { return this->m_y; };
Point(int x, int y) :m_x(x), m_y(y) { }
};
class Math {
public:
Point add(const Point &point1, const Point &point2) {
return Point(point1.m_x + point2.m_x, point1.m_y + point2.m_y);
}
};
Point add(const Point &point1, const Point &point2) {
return Point(point1.m_x + point2.m_x, point1.m_y + point2.m_y);
}
int main() {
Point point1(10, 20);
Point point2(20, 30);
Point point = add(point1, point2);
cout << endl;
getchar();
return 0;
}
內(nèi)部類
如果將類A定義在類C的內(nèi)部劣像,那么類A就是一個內(nèi)部類(嵌套類)
- 支持public、protected摧玫、public耳奕、權(quán)限
- 成員函數(shù)可以直接訪問其外部類對象的所有成員,反過來不行
- 成員函數(shù)可以直接不帶類名诬像、對象名訪問其外部的static成員
- 不會影響外部類的內(nèi)存布局
- 可以在外部類內(nèi)部聲明屋群,在外部類外面進行定義
局部類
在一個函數(shù)內(nèi)部定于的類,被稱為局部類坏挠。它的作用域僅限于所在的函數(shù)內(nèi)部芍躏,其所有的成員必須定義在類內(nèi)部,不允許定義static成員變量降狠,成員函數(shù)不能直接訪問函數(shù)的局部變量对竣,可以訪問static成員變量。
#include <iostream>
using namespace std;
int g_age = 20;
void test() {
int age = 10;
static int s_age = 30;
// 局部類
class Person {
public:
static void run() {
g_age = 30;
s_age = 40;
//age = 100; //內(nèi)部類不允許訪問函數(shù)局部變量
cout << "run()" << endl;
}
};
Person person;
Person::run();
}
int main() {
test();
getchar();
return 0;
}