目录
一.拷贝构造函数
第一种情况:基类没有拷贝构造函数,派生类也没有拷贝构造函数
结果:
原因:
第二种情况:基类没有拷贝构造函数,派生类有拷贝构造函数
结果:
原因:
第三种情况:基类有拷贝构造函数,派生类没有拷贝构造函数
结果:
原因:
第四种情况:基类有拷贝构造函数,派生类有拷贝构造函数
结果:
二.赋值重载
三. 静态成员
结果:
原因:
一.拷贝构造函数
第一种情况:基类没有拷贝构造函数,派生类也没有拷贝构造函数
class Person {
private:
int _id;
public:
Person() :_id(0) { cout << "Create Person()" << endl; }
Person(int id) :_id(id) { cout << "Create Person(int)" << endl; }
~Person() { cout << "Destory Person()" << endl; }
void PrintPersonInfo()const { cout << "id: " << _id << endl; }
};
class Student :public Person {
private:
int _sid;
public:
Student() :_sid(0) { cout << "Create Student" << endl; }
Student(int id,int sd) :Person(id), _sid(sd) { cout << "Create Student(int int)" << endl; }
~Student() { cout << "Destory Student" << endl; }
void PrintStudentInfo()const {
PrintPersonInfo();
cout << "sid: " << _sid << endl;
}
};
int main() {
Student s1(12345,6789);
s1.PrintStudentInfo();
Student s2(s1);
s2.PrintStudentInfo();
}
结果:
原因:
当两者都没有拷贝构造函数时,是按照按位拷贝的方式,用两个指针分别指向s1和s2,将s1的数据一字节一字节拷贝给s2
第二种情况:基类没有拷贝构造函数,派生类有拷贝构造函数
class Person {
private:
int _id;
public:
Person() :_id(0) { cout << "Create Person()" << endl; }
Person(int id) :_id(id) { cout << "Create Person(int)" << endl; }
~Person() { cout << "Destory Person()" << endl; }
void PrintPersonInfo()const { cout << "id: " << _id << endl; }
};
class Student :public Person {
private:
int _sid;
public:
Student() :_sid(0) { cout << "Create Student" << endl; }
Student(int id,int sd) :Person(id), _sid(sd) { cout << "Create Student(int int)" << endl; }
~Student() { cout << "Destory Student" << endl; }
Student(const Student& st) :_sid(st._sid) {
cout << "Copy Create Student" << endl;
}
void PrintStudentInfo()const {
PrintPersonInfo();
cout << "sid: " << _sid << endl;
}
};
int main() {
Student s1(12345,6789);
s1.PrintStudentInfo();
Student s2(s1);
s2.PrintStudentInfo();
}
结果:
原因:
系统在调用派生类的拷贝构造的时候,会先去构建父类,这个我们在上一节说过,因为这里并没有明确派生类中Person这个不具名对象的值,所以调用了Person父类中的空参构造,因此此时值为0;
如果我们此时明确指出Person的值与student中的值是一致的,那么就可以实现。这里用到了赋值兼容原则,有不明白的可以查看上次博客
Student(const Student& st) :Person(st),_sid(st._sid) {
cout << "Copy Create Student" << endl;
}
第三种情况:基类有拷贝构造函数,派生类没有拷贝构造函数
class Person {
private:
int _id;
public:
Person() :_id(0) { cout << "Create Person()" << endl; }
Person(int id) :_id(id) { cout << "Create Person(int)" << endl; }
~Person() { cout << "Destory Person()" << endl; }
Person(const Person& pr) : _id(pr._id) {
cout << "Copy Create Person" << endl;
}
void PrintPersonInfo()const { cout << "id: " << _id << endl; }
};
class Student :public Person {
private:
int _sid;
public:
Student() :_sid(0) { cout << "Create Student" << endl; }
Student(int id,int sd) :Person(id), _sid(sd) { cout << "Create Student(int int)" << endl; }
~Student() { cout << "Destory Student" << endl; }
void PrintStudentInfo()const {
PrintPersonInfo();
cout << "sid: " << _sid << endl;
}
};
int main() {
Student s1(12345,6789);
s1.PrintStudentInfo();
Student s2(s1);
s2.PrintStudentInfo();
}
结果:
原因:
系统在调用Student的拷贝构造时发现不存在,会默认的给出一个拷贝构造函数,这个函数里可以调用父类的拷贝构造函数,因此是可以的
第四种情况:基类有拷贝构造函数,派生类有拷贝构造函数
这里我们必须明确在派生类的构造函数中指出派生类中的不具名对象的值等于所引用的值,会出现切片现
class Person {
private:
int _id;
public:
Person() :_id(0) { cout << "Create Person()" << endl; }
Person(int id) :_id(id) { cout << "Create Person(int)" << endl; }
~Person() { cout << "Destory Person()" << endl; }
Person(const Person& pr) : _id(pr._id) {
cout << "Copy Create Person" << endl;
}
void PrintPersonInfo()const { cout << "id: " << _id << endl; }
};
class Student :public Person {
private:
int _sid;
public:
Student() :_sid(0) { cout << "Create Student" << endl; }
Student(int id,int sd) :Person(id), _sid(sd) { cout << "Create Student(int int)" << endl; }
~Student() { cout << "Destory Student" << endl; }
Student(const Student& st) :Person(st),_sid(st._sid) {
cout << "Copy Create Student" << endl;
}
void PrintStudentInfo()const {
PrintPersonInfo();
cout << "sid: " << _sid << endl;
}
};
int main() {
Student s1(12345,6789);
s1.PrintStudentInfo();
Student s2(s1);
s2.PrintStudentInfo();
}
结果:
二.赋值重载
赋值重载的情况和拷贝构造是一样的,这里就不展开说了。
注意两点:如果两个类都没有赋值语句,就会按位赋值。必须指明不具名对象的值。
class Person {
private:
int _id;
public:
Person() :_id(0) { cout << "Create Person()" << endl; }
Person(int id) :_id(id) { cout << "Create Person(int)" << endl; }
~Person() { cout << "Destory Person()" << endl; }
Person(const Person& pr) : _id(pr._id) {
cout << "Copy Create Person" << endl;
}
Person& operator=(const Person& pr) {
if(this!=&pr){
_id = pr._id;
}
cout << "Person::operator=" << endl;
return *this;
}
void PrintPersonInfo()const { cout << "id: " << _id << endl; }
};
class Student :public Person {
private:
int _sid;
public:
Student() :_sid(0) { cout << "Create Student" << endl; }
Student(int id,int sd) :Person(id), _sid(sd) { cout << "Create Student(int int)" << endl; }
~Student() { cout << "Destory Student" << endl; }
Student(const Student& st) :Person(st),_sid(st._sid) {
cout << "Copy Create Student" << endl;
}
Student& operator=(const Student& st) {
if (this != &st) {
Person::operator=(st);
this->_sid = st._sid;
}
cout << "Student::operator=" << endl;
return *this;
}
void PrintStudentInfo()const {
PrintPersonInfo();
cout << "sid: " << _sid << endl;
}
};
int main() {
Student s1(12345,6789);
s1.PrintStudentInfo();
Student s3;
s3=s1;
s3.PrintStudentInfo();
}
三. 静态成员
class O {
private:
int value;
protected:
static int num;
public:
O(int x = 0) :value(x) {
}
~O() {
}
};
int O::num = 0;
class B :public O {
public :
B() {
cout << "create B: " << ++num << endl;
}
~B() {
cout << "Destory B: " << --num << endl;
}
};
class T:public O{
public:
T() {
cout << "create : " << ++num << endl;
}
~T() {
cout << "Destory : " << --num << endl;
}
};
int main() {
T t[2];
B b[3];
return 0;
}
结果:
原因:
基类中的静态成员被所有派生类所共享,只有一份
如果你想使打印效果为T:1,2 B: 1,2,3 可以考虑类模板
本期就更到这里,谢谢各位宝子阅读。