c++ 拷贝构造函数

来源:互联网 发布:apache php7配置 编辑:程序博客网 时间:2024/06/05 04:22
而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量。

下面看一个类对象拷贝的简单例子。

#include <iostream>#include <string>using namespace std;class Cexample{private:int a;public:Cexample(int  b)//构造函数{a=b;}Cexample(const Cexample& C)//拷贝构造函数{a=C.a;}void Show(){cout<<a<<endl;}};int main(){Cexample A(100);Cexample B=A;B.Show();system("pause");return 0;}

拷贝构造函数的调用时机

1 对象以值传递的方式传入函数参数

2 对象以值传递的方式从函数返回

3 对象需要通过另外一个对象初始化

0 0