马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
怎么理解这里的
类GoodGay里面的下面这两处。
构造函数里面开辟了一个Building指针,然后又创建了一个Building指针 Building* building;
GoodGay()
{
building = new Building; //new什么就返回什么,创建了一个Building指针
}
Building* building;
class Building
{
//goodGay函数是 Building的好朋友,可以访问类内的私有成员
friend class GoodGay;
public:
Building()
{
m_sittingRoom = "客厅";
m_bedRoom = "卧室";
}
string m_sittingRoom;
private:
string m_bedRoom;
};
class GoodGay
{
public:
GoodGay()
{
building = new Building; //new什么就返回什么,创建了一个Building指针
}
Building* building;
void visit()
{
cout << "GoodGay is visting :" << building->m_sittingRoom << endl;
cout << "GoodGay is visting :" << building->m_bedRoom << endl;
}
};
int main()
{
GoodGay gay;
gay.visit();
return 0;
}
Building* building;
GoodGay()
{
building = new Building; //new什么就返回什么,创建了一个Building指针
}
这样你应该就懂了吧,
Building* building; 这是声明
building = new Building; 这是赋值,创建第一个类的实例
|