c++课件第10章指针及引用高级应用.pptxVIP

  • 2
  • 0
  • 约7.2千字
  • 约 35页
  • 2017-06-13 发布于湖北
  • 举报
第10章指针和引用高级应用 -2- 本章内容安排 堆中的数据成员 const修饰指针 数据保护 指针和引用的陷阱 -3- 1、指针数据成员 类中可能会包含指针数据成员,每个指针指向堆中的对象。通常在构造函数(或其它成员函数)中分配内存,在析构函数中释放内存。 含有指针数据成员的类,都要编写析构函数释放内存,编译器提供的缺省析构函数不会释放内存,将造成内存泄漏。 -4- 2、SimpleCat类 class SimpleCat { public: SimpleCat(); ~SimpleCat(); int getAge() const { return *itsAge; } int getWeight()const { return *itsWeight; } void setAge(int age ) { *itsAge=age; } void setWeight(int weight) { *itsWeight=weight; } private: int *itsAge; int *itsWeight; }; 为含有指针成员的类编写构造与析构函数。 -5- SimpleCat类 SimpleCat::SimpleCat() { itsAge = new int(2); itsWeight = new int(5); } SimpleCat::~Si

文档评论(0)

1亿VIP精品文档

相关文档