【发布时间】:2021-03-13 23:39:33
【问题描述】:
This is the original lab question
#include <iostream>
#include <vector>
#include <new>
using namespace std;
class c{
public:
c(){
int i = 0;
vector <c*> objects;
while (true){
objects.push_back(new c());
cout << "Object " << i+1 << "is created.";
i++;
if (i == 10) {
throw i+1;}}}
};
int main(){
while (true){
try {c obj;}
catch (int x) {cout << "Object" << x << "cannot be created as only 10 objects are allowed";}}
return 0;
}
这就是我所做的。 我有两个问题。
1.为什么它不打印任何输出?
2。如何添加回收此内存的静态成员函数?
这是 c++,在此先感谢。
【问题讨论】:
-
任务说你应该实现一个 operator new 而不是把所有东西都放在构造函数中
标签: c++ exception memory static output