#include <iostream>
using namespace std;
//对象成员指针就是一个类里面的类是一个指针
//对象指针:类指针,创建的是一个类类型的指针
class coordinate
{
public:
coordinate(int xx,int yy);
~coordinate();
int x;
int y;
};
coordinate::coordinate(int xx,int yy)
{
x = xx;
y = yy;
cout<<"coordinate()"<<x<<' '<<y<<endl;
}
coordinate::~coordinate()
{
cout<< "~coordinate() "<<x<<' '<< y <<endl;
}
class line
{
public:
line(int x1, int y1,int x2,int y2);
~line();
private:
coordinate *a = NULL;//对象成员指针
coordinate *b = NULL;//对象成员指针
};
line::line(int x1, int y1,int x2,int y2)
{
a = new coordinate(x1,y1);
b = new coordinate(x2,y2);
cout<<"line()"<<endl;
}
line::~line()
{
delete a;
a = NULL;
delete b;
b = NULL;
cout<<"~line()"<<endl;
}
int main(void)
{
line x(1,2,3,4);//"释放顺序与对象成员不同"
/*
coordinate *p = new coordinate(4,5); //对象指针
cout<<p->x<<' '<<(*p).y<<endl;
delete p;
*/
cout<<sizeof(line)<<endl;//"line的大小是两个指针的大小"
}
c++快速入门 对象指针,对象成员指针
最新推荐文章于 2024-05-17 22:58:01 发布