/* * main.cpp * * Created on: Apr 7, 2016 * Author: lizhen */ #include <iostream> #include "MySqrt.h" #include <math.h> //#include "findMedianSortedArrays.h" //#include "myfindMedianSortedArrays.h" //#include "longestConsecutive.h" //#include "threeSum.h" //#include "threesumclosest.h" //#include "nextPermutation.h" //#include "trap.h" //#include "rotateImage.h" //#include "setMatrixZeroes.h" #include "treeNode.h" #include <vector> #include "preorderT.h" using namespace std; class HasPtr{ /*添加拷贝复制运算符和析构函数,编写一个程序以不同的方式调用HasPtr的对象,将他们作为 * 非引用和引用参数传递; * 动态分配它们 * 将他们存放于容器中 * ----》 * 观察程序的输出,知道你确认理解了什么时候使用拷贝控制成员, * 以及为什么会使用它们。 * note: * 编译器可以略过对拷贝构造函数的调用。 * string null_book = "999" * ---> * string null_book("999") * */ public: HasPtr(string *ps,int i):ps(ps),i(i){ cout<<"HasPtr()"<<endl; } HasPtr(const string &s = string()): ps(new string(s)),i(0){ cout<<" hasptr()"<<endl; } //拷贝构造函数 HasPtr(const HasPtr &isCopy){ cout<<"hasptr(const HasPtr& isCopy)"<<endl; } //拷贝赋值运算符 HasPtr& operator=(const HasPtr &isAssign){ ps = isAssign.ps; i = isAssign.i; cout<<"HasPtr& operator=(const HasPtr &isAssign)"<<endl; return *this; } //析构函数 ~HasPtr(){ cout<<"~HasPtr()"<<endl; delete ps; } private: string *ps; int i ; }; class X{ /*添加拷贝复制运算符和析构函数,编写一个程序以不同的方式调用HasPtr的对象,将他们作为 * 非引用和引用参数传递; * 动态分配它们 * 将他们存放于容器中 * ----》 * 观察程序的输出,知道你确认理解了什么时候使用拷贝控制成员, * 以及为什么会使用它们 */ private: int x; public: X(int i):x(i){ cout<<"x(int i)"<<endl; } X(){ x = 0; cout<<"x()"<<endl; } X(const X &xx):x(xx.x){ cout<<"X(const X &xx)"<<endl; } X& operator=(const X &xx){ x = xx.x; cout<<"X& operator=(const X &xx)"<<endl; return *this; } ~X(){ cout<<"~X()"<<endl; } }; void arg1(X &x){ cout<<"arg1"<<endl; } void arg2(X x){ cout<<"arg2"<<endl; } X arg3(X &x){ cout<<"arg3"<<endl; return x; } X& arg4(X &x){ cout<<"arg4"<<endl; return x; } int main(){ { X x(1); cout<<"1--------"<<endl<<endl; } { X x(1); arg1(x); cout<<"2--------"<<endl<<endl; } { X x(1); arg2(x); cout<<"3---------"<<endl<<endl; } { X *ptr = new X(2); cout<<"4---------"<<endl<<endl; } { X x1(2); X x2(3); x2 = arg3(x1); cout<<"5----------"<<endl<<endl; } { X x1(2); X x2(3); x2 = arg3(x1); cout<<"6----------"<<endl<<endl; } { vector<X> xvc(4);//4 xvc.push_back(X(1)); xvc.push_back(X(2)); xvc.push_back(X(3)); xvc.erase(xvc.begin()); xvc.push_back(X(4)); } return 0; }
相关文章: