首页 > 学院 > 开发设计 > 正文

cpp 12.9

2019-11-11 05:01:08
字体:
来源:转载
供稿:网友

placenew2.cpp

#include<iostream>#include<string>#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 << words << "constructed/n";	}	~JustTesting() { cout << words << "destroyed/n"; }	void Show() const{ cout << words << ", " << number << endl; }};int main(){	char*buffer = new char[BUF];	JustTesting*pc1, *pc2;	pc1 = new(buffer) JustTesting;	pc2 = new JustTesting("heap1", 20);	cout << "Memory block address:/n" << "buffer: "		<< (void*)buffer << " heap: " << pc2 << endl;	cout << "Memory contents:/n";	cout << pc1 << ": ";	pc1->Show();	cout << pc2 << ": ";	pc2->Show();	JustTesting *pc3, *pc4;	pc3 = new(buffer + sizeof(JustTesting))		JustTesting("Better Idea", 6);	pc4 = new JustTesting("Heap2", 10);	cout << "Memory contemts:/n";	cout << pc3 << ": ";	pc3->Show();	cout << pc4 << ": ";	pc4->Show();	delete pc2;	delete pc4;	pc3->~JustTesting();	pc1->~JustTesting();	delete[] buffer;	cout << "Done/n";		system("pause");	return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表