#include <iostream>
using namespace std;
class Student
{
public:
Student (int a)
{
m_a = a;
}
Student (const Student &obj)
{
m_a = obj.m_a;
cout << "拷贝构造被调用" << endl;
}
void show()
{
cout << "m_a = " << m_a << endl;
}
private:
int m_a;
// int m_b;
// chat *m_name;
};
int main()
{
Student s = 10;
Student s1 = s;
s1.show();
return 0;
}
#include <iostream>
using namespace std;
class Student
{
public:
Student (int a,int b,int c)
{
m_a = a;
m_b = b;
m_pa =new int(c);
}
Student (const Student &obj)
{
m_a = obj.m_a;
m_b = obj.m_b;
m_pa = new int ;
*m_pa = *obj.m_pa;
cout << "拷贝构造函数被调用" << endl;
}
~Student()
{
cout << "析构函数被调用" << endl;
printf ("m_pa = %p\n", m_pa);
if (m_pa != nullptr)
delete m_pa;
m_pa = nullptr;
}
void show()
{
cout << "m_a = " << m_a << ",m_b" << m_b << ",*m_pa = " << *m_pa << endl;
}
private:
int m_a;
int m_b;
int *m_pa;
};
int main()
{
Student s(10,20,30);
Student s1 = s;
s1.show();
return 0;
}