C++继承中的对象构造与析构和赋值重载详解
短信预约 -IT技能 免费直播动态提醒
一、构造/析构顺序及继承性
class A
{
private:
int _a;
public:
A(int a = 0): _a(a)
{
cout << "A()" << this << endl;
}
~A()
{
cout << "~A()"<< this <<endl;
}
};
class B : public A
{
private:
int _b;
public:
B(int b): _b(b), A()
{
cout << "B()" << this << endl;
}
~B()
{
cout << "~B()"<< this <<endl;
}
};
结论:
1.构造顺序:先构造基类,后构造派生类
2.析构顺序:先析构派生类,后析构基类
二、拷贝构造的继承性
class A
{
private:
int _a;
public:
A(int a = 0): _a(a)
{
cout << "A()" << this << endl;
}
A(const A& class="lazy" data-src): _a(class="lazy" data-src._a)
{
cout << "A(const A& class="lazy" data-src)"<< this << endl;
}
~A()
{
cout << "~A()"<< this <<endl;
}
};
class B : public A
{
private:
int _b;
public:
B(int b): _b(b), A()
{
cout << "B()" << this << endl;
}
B(const B& class="lazy" data-src): _b(class="lazy" data-src._b)
{
cout << "B(const B& class="lazy" data-src)" << this << endl;
}
~B()
{
cout << "~B()"<< this <<endl;
}
};
结论:
1.先调用基类缺省的构造函数,后调用派生类的拷贝构造函数
2.若派生类没有缺省构造函数A(),就会报错
疑惑:如何去调用基类的拷贝构造而不是缺省构造
#include<iostream>
using namespace std;
class A
{
private:
int _a;
public:
A(int a = 0) : _a(a)
{
cout << "A()" << this << endl;
}
A(const A& class="lazy" data-src) : _a(class="lazy" data-src._a)
{
cout << "A(const A& class="lazy" data-src)" << this << endl;
}
~A()
{
cout << "~A()" << this << endl;
}
};
class B : public A
{
private:
int _b;
public:
B(int b) : _b(b), A()
{
cout << "B()" << this << endl;
}
B(const B& class="lazy" data-src) : _b(class="lazy" data-src._b), A(class="lazy" data-src) //发生赋值兼容规则(切片)
{
cout << "B(const B& class="lazy" data-src)" << this << endl;
}
~B()
{
cout << "~B()" << this << endl;
}
};
int main()
{
B b(10);
B b1(b);
return 0;
}
结果:
将B类型class="lazy" data-src传递给A类型的A(const A& class="lazy" data-src)拷贝构造函数,发生了赋值兼容规则(切片现象)
三、赋值重载不具有继承性
#include<iostream>
using namespace std;
class A
{
private:
int _a;
public:
A(int a = 0) : _a(a)
{
cout << "A()" << this << endl;
}
A(const A& class="lazy" data-src) : _a(class="lazy" data-src._a)
{
cout << "A(const A& class="lazy" data-src)" << this << endl;
}
A& operator=(const A& class="lazy" data-src)
{
if(this != &class="lazy" data-src)
{
_a = class="lazy" data-src._a;
cout << "A& operator=(const A& class="lazy" data-src)" << endl;
}
}
~A()
{
cout << "~A()" << this << endl;
}
};
class B : public A
{
private:
int _b;
public:
B(int b) : _b(b), A()
{
cout << "B()" << this << endl;
}
B(const B& class="lazy" data-src) : _b(class="lazy" data-src._b), A(class="lazy" data-src) //发生赋值兼容规则(切片)
{
cout << "B(const B& class="lazy" data-src)" << this << endl;
}
B& operator=(const B& class="lazy" data-src)
{
if(this != &class="lazy" data-src)
{
_b = class="lazy" data-src._b;
cout << "B& operator=(const B& class="lazy" data-src)" << endl;
}
}
~B()
{
cout << "~B()" << this << endl;
}
};
int main()
{
B b1(10);
B b2(20);
b1 = b2;
return 0;
}
结论:默认情况下仅仅调用了派生类的对象的赋值重载,并未调用基类的赋值重载。
解决方案:
#include<iostream>
using namespace std;
class A
{
private:
int _a;
public:
A(int a = 0) : _a(a)
{
cout << "A()" << this << endl;
}
A(const A& class="lazy" data-src) : _a(class="lazy" data-src._a)
{
cout << "A(const A& class="lazy" data-src)" << this << endl;
}
A& operator=(const A& class="lazy" data-src)
{
if(this != &class="lazy" data-src)
{
_a = class="lazy" data-src._a;
cout << "A& operator=(const A& class="lazy" data-src)" << endl;
}
}
~A()
{
cout << "~A()" << this << endl;
}
};
class B : public A
{
private:
int _b;
public:
B(int b) : _b(b), A()
{
cout << "B()" << this << endl;
}
B(const B& class="lazy" data-src) : _b(class="lazy" data-src._b), A(class="lazy" data-src) //发生赋值兼容规则(切片)
{
cout << "B(const B& class="lazy" data-src)" << this << endl;
}
B& operator=(const B& class="lazy" data-src)
{
if(this != &class="lazy" data-src)
{
*(A*)this = class="lazy" data-src; //将调用基类赋值重载
_b = class="lazy" data-src._b;
cout << "B& operator=(const B& class="lazy" data-src)" << endl;
}
}
~B()
{
cout << "~B()" << this << endl;
}
};
int main()
{
B b1(10);
B b2(20);
b1 = b2;
return 0;
}
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程网的更多内容!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341