1 #include <iostream> 2 using namespace std; 3 class A{ 4 public: 5 A(void):m_a(100),m_i(20){cout << "構(gòu)造函數(shù)" << endl;} //構(gòu)造函數(shù) 6 ~A(void){cout << "析構(gòu)函數(shù)" << endl;} //析構(gòu)函數(shù) 7 A(A &a); //拷貝構(gòu)造函數(shù) 8 private: 9 int m_a; 10 int m_i; 11 }; 12 A::A(A &a){ 13 m_a = a.m_a; 14 m_i = a.m_i; 15 cout << "拷貝構(gòu)造函數(shù)" << endl; 16 cout << "m_a:" << m_a << endl; 17 cout << "m_i:" << m_i << endl; 18 19 this->m_a = ++m_a; 20 this->m_i = --m_i; 21 cout << "this->m_a:" << this->m_a << endl; 22 cout << "this->m_i:" << this->m_i << endl; 23 } ? 24 int main(void){ 25 A a1; 26 A a2(a1); 27 28 return 0; 29 } ? ?tarena@ubuntu:~/cplus/day08$ g++ text.cpp tarena@ubuntu:~/cplus/day08$ ./a.out 構(gòu)造函數(shù) 拷貝構(gòu)造函數(shù) m_a:100 m_i:20 this->m_a:101 this->m_i:19 析構(gòu)函數(shù) 析構(gòu)函數(shù) |
|