Files
912-notes/c++ note/chp13/chp13.cpp

59 lines
892 B
C++

#include <iostream>
#include <list>
#include <string>
#include <forward_list>
using namespace std;
class HasPtr {
public:
explicit HasPtr(const std::string &s = std::string()) :
ps(new std::string(s)), i(0) {}
//copy constructor
HasPtr(const HasPtr& ptr);
//copy assignment operator
HasPtr& operator=(const HasPtr& ptr);
//destructor
~HasPtr(){
delete ps;
}
private:
std::string *ps;
int i;
};
HasPtr::HasPtr(const HasPtr& ptr){
ps = new string(*ptr.ps);
i = ptr.i;
}
HasPtr& HasPtr::operator=(const HasPtr& ptr){
delete ps;
ps = new string(*ptr.ps);
i = ptr.i;
return *this;
}
HasPtr f(HasPtr fp){
HasPtr ret = fp;
return ret;
}
int main(){
HasPtr ptr1("2020");
HasPtr ptr2("2019");
HasPtr ptr3 = ptr2;
forward_list<int> li({ 4, 5, 6 });
for (int e : li)
cout << e << " ";
ptr2 = ptr1;
ptr3 = ptr1;
f(ptr1);
system("pause");
return 0;
}