摘要: 上题中,使用定位new分配内存中存在两个问题。1,pc3创建的时候覆盖掉了pc1,这是因为new对象时提供的地址(buffer)相同,应当给后创建的对象提供的地址一个偏移量sizeof(JustTesting);2,delete [] buffer 未能调用定位new的两个对象pc1,pc3的析构函数,需要显式的调用他们。程序清单如下:// placenew2.cpp#include #include #include using namespace std;const int BUF = 512;class JustTesting{private: string words; int num 阅读全文
posted @ 2013-05-15 13:22 庄懂 阅读(138) 评论(0) 推荐(0) 编辑
摘要: 试用使用定位new运算符给对象分配内存。程序清单如下:// placenew1.cpp#include #include #include // 使用定位new运算符using namespace std;const int BUF = 512;class JustTesting{private: string words; int number;public: JustTesting(const string& s = "Just Testing", int n = 0) { words = s; number = n; cout Show(); cout Sho 阅读全文
posted @ 2013-05-15 13:17 庄懂 阅读(117) 评论(0) 推荐(0) 编辑
摘要: 续上题,用指向对象的指针改写Example6。程序清单如下:// sayings2.cpp#include #include #include #include "string1.h"const int ArSize = 10;const int MaxLen = 81;int main(){ using namespace std; String name; cout > name; cout :\n"; String sayings[ArSize]; char temp[MaxLen]; int i; for (i = 0; i 0) { cout le 阅读全文
posted @ 2013-05-15 13:13 庄懂 阅读(144) 评论(0) 推荐(0) 编辑