#include <iostream> using namespace std; class A { public: A(int a = 1, int b = 2) //带默认形参值的构造函数 { x = a; y = b; } A(A& obj) //拷贝构造函数 { x = obj.x; y = obj.y; cout << "copy_constructor called." << endl; } ~A() { cout << "destructor called.\n"; } int Getx() { return x; } int Gety() { return y; } A& operator=(A obj) { std::cout << "assignment operator called." << endl; x = obj.x, y = obj.y; std::cout << "obj address :" << &obj << std::endl; return *this; } private: int x, y; }; A& fun() { A * a = new A(15, 30); std::cout << "pointer address :" << a << std::endl; return *a; } void main() { A b; cout << b.Getx() << ", " << b.Gety() << endl; b = fun(); cout << b.Getx() << ", " << b.Gety() << endl; std::cout << "b address :" << &b << std::endl; }
拷贝构造函数示例(重要)
猜你喜欢
转载自javaeye-hanlingbo.iteye.com/blog/2408192
今日推荐
周排行