#include<iostream>
#include<string>
using namespace std;
class Phone {
public:
Phone(string pname) {
m_pname = pname;
cout << "Phone的构造函数调用" << endl;
}
string m_pname;
};
class Person {
public:
Person(string name, string pname):m_name(name),m_phone(pname) {
cout << "person的构造函数调用" << endl;
}
string m_name;
Phone m_phone;//上一个类的对象
};
void test01() {
Person p("张三","苹果");
cout <<p.m_name<< "拿着 "<<p.m_phone.m_pname<< endl;
}
int main() {
test01();
return 0;
}
可以从运行结果中看出当一个类中有其他类的对象作为本类成员,先构造其他类对象,然后再构造本身。
而析构的顺序与构造相反